|
2 | 2 | using Source.Common.Formats.BSP; |
3 | 3 | using Source.Common.GarrysMod; |
4 | 4 | using Source.Common.GarrysMod.Lua; |
| 5 | +using Source.Common.Hashing; |
5 | 6 |
|
6 | 7 | #if CLIENT_DLL |
7 | 8 | namespace Game.Client.GarrysMod; |
@@ -215,5 +216,79 @@ static int LightStyle(ILuaInterface lua) { |
215 | 216 | return 0; |
216 | 217 | } |
217 | 218 | #endif |
| 219 | + |
| 220 | +#if CLIENT_DLL |
| 221 | + [LuaFunction] |
| 222 | + static int WriteDupe(ILuaInterface lua) { |
| 223 | + ReadOnlySpan<byte> data = lua.GetStringBytes(1); |
| 224 | + ReadOnlySpan<byte> jpeg = lua.GetStringBytes(2); |
| 225 | + string name = MD5Value.Compute(data).ToString(); |
| 226 | + |
| 227 | + byte[] buffer = new byte[4 + data.Length]; |
| 228 | + buffer[0] = (byte)'D'; |
| 229 | + buffer[1] = (byte)'U'; |
| 230 | + buffer[2] = (byte)'P'; |
| 231 | + buffer[3] = (byte)'3'; |
| 232 | + data.CopyTo(buffer.AsSpan(4)); |
| 233 | + |
| 234 | + string path = $"dupes/{name}.dupe"; |
| 235 | + if (!filesystem.FileExists(path, "MOD")) |
| 236 | + filesystem.WriteFile(path, "MOD", buffer); |
| 237 | + |
| 238 | + filesystem.WriteFile($"dupes/{name}.jpg", "MOD", jpeg); |
| 239 | + lua.PushBool(true); |
| 240 | + return 1; |
| 241 | + } |
| 242 | + |
| 243 | + [LuaFunction] |
| 244 | + static int OpenDupe(ILuaInterface lua) { |
| 245 | + string path = new(lua.CheckString(1)); |
| 246 | + if (!GMOD.IsValidPath(path)) |
| 247 | + return 0; |
| 248 | + |
| 249 | + if (path.Length > 13 && strncmp(path, "content/4000/", 13) == 0) { |
| 250 | + int slash = path.IndexOf('/', 13); |
| 251 | + if (slash != -1 && ulong.TryParse(path.AsSpan(13, slash - 13), out ulong workshopID) && workshopID != 0) |
| 252 | + path = filesystem.Addons().GetAddonFilepath(workshopID, false); |
| 253 | + } |
| 254 | + |
| 255 | + long size = filesystem.Size(path, "MOD"); |
| 256 | + if (size < 4) |
| 257 | + return 0; |
| 258 | + |
| 259 | + byte[] file = new byte[size]; |
| 260 | + if (!filesystem.ReadFile(path, "MOD", file, 0)) |
| 261 | + return 0; |
| 262 | + |
| 263 | + if (file[0] != 'D' || file[1] != 'U' || file[2] != 'P') |
| 264 | + return 0; |
| 265 | + |
| 266 | + int headerSize; |
| 267 | + if (file[3] == '1') |
| 268 | + headerSize = 16; |
| 269 | + else if (file[3] == '3') |
| 270 | + headerSize = 4; |
| 271 | + else |
| 272 | + return 0; |
| 273 | + |
| 274 | + int dataSize = (int)size - headerSize; |
| 275 | + if ((uint)(dataSize - 1) > 2097151) { |
| 276 | + Warning($"Dupe Filesize is crazy ({dataSize})\n"); |
| 277 | + return 0; |
| 278 | + } |
| 279 | + |
| 280 | + lua.PushString(file.AsSpan(headerSize, dataSize)); |
| 281 | + LuaObject data = new(); |
| 282 | + data.SetFromStack(-1); |
| 283 | + lua.Pop(1); |
| 284 | + |
| 285 | + LuaTable table = new(null, 0); |
| 286 | + table.SetMember("data", data); |
| 287 | + table.Push(); |
| 288 | + table.UnReference(); |
| 289 | + data.UnReference(); |
| 290 | + return 1; |
| 291 | + } |
| 292 | +#endif |
218 | 293 | } |
219 | 294 | #endif |
0 commit comments