Skip to content

Commit 1384ea5

Browse files
committed
Dupes
1 parent ea9f255 commit 1384ea5

4 files changed

Lines changed: 90 additions & 2 deletions

File tree

‎Game.Shared/GarrysMod/LuaEngine.cs‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Source.Common.Formats.BSP;
33
using Source.Common.GarrysMod;
44
using Source.Common.GarrysMod.Lua;
5+
using Source.Common.Hashing;
56

67
#if CLIENT_DLL
78
namespace Game.Client.GarrysMod;
@@ -215,5 +216,79 @@ static int LightStyle(ILuaInterface lua) {
215216
return 0;
216217
}
217218
#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
218293
}
219294
#endif

‎Game.Shared/GarrysMod/LuaGlobalFunctions.cs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,17 @@ static void require(string name) {
273273

274274
#if GAME_DLL
275275
static ConVar? hostnameCvar;
276+
277+
static readonly ConVarRef sv_logecho = new("sv_logecho");
278+
279+
[LuaGlobal]
280+
static int ServerLog(ILuaInterface lua) {
281+
if (sv_logecho.GetInt() == 0)
282+
return 0;
283+
Dbg.Msg($"ServerLog: {lua.GetString(1)}");
284+
engine.LogPrint(lua.GetString(1));
285+
return 0;
286+
}
276287
#endif
277288

278289
#if !MENU_DLL

‎Game.Shared/GarrysMod/LuaUtil.cs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,6 @@ static TraceFilterLua TraceFilterFromTable(LuaObject data) {
553553
return filter;
554554
}
555555
#endif
556-
#if CLIENT_DLL || MENU_DLL
557556
static void TableToKeyValues(ILuaInterface lua, ILuaObject table, System.Text.StringBuilder output, string name, int depth) {
558557
output.Append('\t', depth).Append('"').Append(name).Append("\"\n");
559558
output.Append('\t', depth).Append("{\n");
@@ -904,7 +903,6 @@ static int JSONToTable(ILuaInterface lua) {
904903
}
905904
return 1;
906905
}
907-
#endif
908906

909907
#if GAME_DLL
910908
[LuaFunction]

‎Source.Engine/Client/ClientState.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,11 @@ public override bool SetSignonState(SignOnState state, int count) {
559559
CL.FullyConnected();
560560
if (NetChannel != null) {
561561
NetChannel.SetTimeout(cl_timeout.GetDouble());
562+
#if GMOD_DLL
563+
NetChannel.SetMaxBufferSize(true, Protocol.MAX_PAYLOAD);
564+
#else
562565
NetChannel.SetMaxBufferSize(true, Protocol.MAX_DATAGRAM_PAYLOAD);
566+
#endif
563567
}
564568

565569
HostState.OnClientConnected();

0 commit comments

Comments
 (0)