Skip to content

Commit dc50143

Browse files
remyCasesNylux
authored andcommitted
[Major] new in game log for msl, seems to work correctly
1 parent 56a9b19 commit dc50143

File tree

3 files changed

+123
-30
lines changed

3 files changed

+123
-30
lines changed

ModLoader.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ public static void LoadWeapon(Type type)
236236
}
237237
public static void PatchFile()
238238
{
239+
// add new msl log function
240+
LogUtils.InjectLog();
239241
PatchInnerFile();
240242
PatchMods();
241243
// add the new loot related functions if there is any

ModUtils/LogUtils.cs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Serilog;
4+
using Newtonsoft.Json;
5+
using System.IO;
6+
using UndertaleModLib.Models;
7+
8+
namespace ModShardLauncher;
9+
10+
public static class LogUtils
11+
{
12+
public static void InjectLog()
13+
{
14+
string mslLog = @"function scr_msl_log(argument0)
15+
{
16+
var time = date_datetime_string(date_current_datetime());
17+
if (global._msl_log != noone)
18+
{
19+
if (global._msl_log.buf != noone)
20+
{
21+
buffer_write(global._msl_log.buf, buffer_text, ""["" + time + ""]: "" + argument0 + ""\n"");
22+
}
23+
else
24+
{
25+
scr_actionsLogUpdate(""msl log buff does not exist. Please report that bug with the MSL devs."");
26+
}
27+
}
28+
else
29+
{
30+
scr_actionsLogUpdate(""msl log does not exist. Please report that bug with the MSL devs."");
31+
}
32+
33+
if (global._msl_log.timer == noone || !instance_exists(global._msl_log.timer))
34+
{
35+
var t = instance_create(0, 0, o_msl_timer);
36+
t.end_time = 10;
37+
t.func = gml_Script_scr_msl_log_save;
38+
39+
global._msl_log.timer = t.id;
40+
}
41+
42+
}";
43+
44+
string mslLogSave = @"function scr_msl_log_save()
45+
{
46+
buffer_save_async(global._msl_log.buf, global._msl_log.name, 0, 16384);
47+
}";
48+
49+
UndertaleGameObject timer = Msl.AddObject("o_msl_timer", isPersistent: true);
50+
Msl.AddNewEvent(timer, @"
51+
func = -4;
52+
end_time = 0;
53+
cumulative_time = 0;",
54+
EventType.Create, 0);
55+
56+
Msl.AddNewEvent(timer, @"
57+
cumulative_time += delta_time / 1000000;
58+
if (cumulative_time > end_time)
59+
{
60+
if (func != noone)
61+
{
62+
script_execute(func)
63+
}
64+
instance_destroy();
65+
}",
66+
EventType.Step, 0);
67+
68+
UndertaleGameObject log = Msl.AddObject("o_msl_log", isPersistent: true);
69+
Msl.AddNewEvent(log, @"
70+
buf = buffer_create(16384, buffer_wrap, 1);
71+
72+
var curr_time = date_current_datetime();
73+
var format_time = string(date_get_year(curr_time)) + string(date_get_month(curr_time)) + string(date_get_day(curr_time)) + ""_"" + string(date_get_hour(curr_time)) + string(date_get_minute(curr_time));
74+
name = ""Logs/msl_log_"" + format_time + "".txt"";
75+
76+
timer = -4
77+
",
78+
EventType.Create, 0);
79+
80+
Msl.AddFunction(mslLogSave, "scr_msl_log_save");
81+
Msl.AddFunction(mslLog, "scr_msl_log");
82+
Msl.LoadGML(Msl.EventName("o_gameLoader", EventType.Create, 0))
83+
.MatchAll()
84+
.InsertBelow(@"
85+
if (!directory_exists(""Logs""))
86+
{
87+
directory_create(""Logs"");
88+
}
89+
global._msl_log = instance_create(0, 0, o_msl_log);
90+
")
91+
.Save();
92+
}
93+
}

ModUtils/LootUtils.cs

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using Serilog;
5-
using UndertaleModLib.Models;
64
using Newtonsoft.Json;
75
using System.IO;
8-
using Microsoft.Win32;
96

107
namespace ModShardLauncher
118
{
@@ -115,7 +112,7 @@ public static void InjectLootScripts()
115112
}
116113
else
117114
{
118-
scr_actionsLogUpdate(""invalid object "" + string(objectName));
115+
scr_msl_log(""invalid object "" + string(objectName));
119116
}
120117
}
121118
else
@@ -127,7 +124,7 @@ public static void InjectLootScripts()
127124
}
128125
else
129126
{
130-
scr_actionsLogUpdate(""invalid object "" + string(objectName));
127+
scr_msl_log(""invalid object "" + string(objectName));
131128
}
132129
}
133130
}
@@ -160,11 +157,11 @@ public static void InjectLootScripts()
160157
var min_lvl = scr_globaltile_dungeon_get(""mob_lvl_min"");
161158
var max_lvl = scr_globaltile_dungeon_get(""mob_lvl_max"");
162159
var tier = floor(((max_lvl + min_lvl) / 2));
163-
scr_actionsLogUpdate(""current tier: "" + string(tier));
160+
scr_msl_log(""current tier: "" + string(tier));
164161
165162
if (!variable_struct_exists(refData, objectName))
166163
{
167-
scr_actionsLogUpdate(""cant find object "" + objectName);
164+
scr_msl_log(""cant find object "" + objectName);
168165
file_text_close(refFile);
169166
return -4;
170167
}
@@ -173,31 +170,31 @@ public static void InjectLootScripts()
173170
174171
if (!variable_struct_exists(refStruct, ""DefaultTable""))
175172
{
176-
scr_actionsLogUpdate(""cant find DefaultTable"");
173+
scr_msl_log(""cant find DefaultTable"");
177174
file_text_close(refFile);
178175
return -4;
179176
}
180177
var defaultTable = variable_struct_get(refStruct, ""DefaultTable"");
181178
182179
if (!variable_struct_exists(refStruct, ""Ids""))
183180
{
184-
scr_actionsLogUpdate(""cant find Ids"");
181+
scr_msl_log(""cant find Ids"");
185182
file_text_close(refFile);
186183
return -4;
187184
}
188185
var idsStruct = variable_struct_get(refStruct, ""Ids"");
189186
190-
if (variable_struct_exists(idsStruct, argument0.id))
187+
if (variable_struct_exists(idsStruct, string(argument0.id)))
191188
{
192189
var referenceLootTable = variable_struct_get(idsStruct, argument0.id);
193-
scr_actionsLogUpdate(""ref: "" + referenceLootTable);
190+
scr_msl_log(""ref with id: "" + referenceLootTable);
194191
file_text_close(refFile);
195192
return referenceLootTable;
196193
}
197194
198195
if (!variable_struct_exists(refStruct, ""Tiers""))
199196
{
200-
scr_actionsLogUpdate(""cant find Tiers"");
197+
scr_msl_log(""cant find Tiers"");
201198
file_text_close(refFile);
202199
return -4;
203200
}
@@ -207,7 +204,7 @@ public static void InjectLootScripts()
207204
208205
for (var i = 0; i < array_length(tiers); i++;)
209206
{
210-
if (tier < tiers[i])
207+
if (tier < real(tiers[i]))
211208
{
212209
indexTier = i - 1;
213210
break;
@@ -221,13 +218,14 @@ public static void InjectLootScripts()
221218
if (indexTier == -1)
222219
{
223220
var referenceLootTable = defaultTable;
221+
scr_msl_log(""ref with default: "" + referenceLootTable);
224222
}
225223
else
226224
{
227225
var referenceLootTable = variable_struct_get(tiersStruct, tiers[indexTier]);
226+
scr_msl_log(""ref with tier: "" + referenceLootTable);
228227
}
229228
230-
scr_actionsLogUpdate(""ref: "" + referenceLootTable);
231229
file_text_close(refFile);
232230
return referenceLootTable;
233231
}";
@@ -238,7 +236,7 @@ public static void InjectLootScripts()
238236
|| !variable_struct_exists(argument0, ""ListRarity"")
239237
|| !variable_struct_exists(argument0, ""ListDurability""))
240238
{
241-
scr_actionsLogUpdate(""no ItemsTable data"");
239+
scr_msl_log(""no ItemsTable data"");
242240
return 0;
243241
}
244242
@@ -251,7 +249,7 @@ public static void InjectLootScripts()
251249
if (size_array != array_length(rarity) ||
252250
size_array != array_length(durability))
253251
{
254-
scr_actionsLogUpdate(""List with incorrect size"");
252+
scr_msl_log(""List with incorrect size"");
255253
return 0;
256254
}
257255
@@ -268,7 +266,7 @@ public static void InjectLootScripts()
268266
if (!variable_struct_exists(argument0, ""ItemsTable"")
269267
|| !variable_struct_exists(argument0, ""ListWeight""))
270268
{
271-
scr_actionsLogUpdate(""no randomLoot data"");
269+
scr_msl_log(""no randomLoot data"");
272270
return 0;
273271
}
274272
@@ -279,7 +277,7 @@ public static void InjectLootScripts()
279277
|| !variable_struct_exists(itemsTable, ""ListRarity"")
280278
|| !variable_struct_exists(itemsTable, ""ListDurability""))
281279
{
282-
scr_actionsLogUpdate(""no randomLoot data"");
280+
scr_msl_log(""no randomLoot data"");
283281
return 0;
284282
}
285283
@@ -304,10 +302,10 @@ public static void InjectLootScripts()
304302
totalWeight += weight[_i];
305303
}
306304
}
307-
scr_actionsLogUpdate(""totalWeight "" + string(totalWeight));
305+
scr_msl_log(""totalWeight "" + string(totalWeight));
308306
309307
var randomWeight = irandom(totalWeight - 1);
310-
scr_actionsLogUpdate(""randomWeight "" + string(randomWeight));
308+
scr_msl_log(""randomWeight "" + string(randomWeight));
311309
var cumulativeWeight = 0;
312310
var index = -1;
313311
@@ -327,12 +325,12 @@ public static void InjectLootScripts()
327325
328326
if (index != -1)
329327
{
330-
scr_actionsLogUpdate(""found "" + string(index));
328+
scr_msl_log(""found "" + string(index));
331329
scr_msl_resolve_items(items[index], rarity[index], durability[index], argument1, argument2);
332330
}
333331
else
334332
{
335-
scr_actionsLogUpdate(""found empty"");
333+
scr_msl_log(""found empty"");
336334
}
337335
}
338336
@@ -342,12 +340,12 @@ public static void InjectLootScripts()
342340
string mslLootFunction = @"function scr_msl_resolve_loot_table(argument0, argument1)
343341
{
344342
var objectName = object_get_name(argument0.object_index);
345-
scr_actionsLogUpdate(""instance: "" + string(argument0.id) + "" of "" + objectName);
343+
scr_msl_log(""instance: "" + string(argument0.id) + "" of "" + objectName);
346344
347345
var referenceLootTable = scr_msl_resolve_refence_table(argument0);
348346
if (referenceLootTable == noone)
349347
{
350-
scr_actionsLogUpdate(""Reference Table resolution failed"");
348+
scr_msl_log(""Reference Table resolution failed"");
351349
return 0;
352350
}
353351
@@ -357,30 +355,30 @@ public static void InjectLootScripts()
357355
358356
if (!variable_struct_exists(data, referenceLootTable))
359357
{
360-
scr_actionsLogUpdate(""cant find ref "" + referenceLootTable);
358+
scr_msl_log(""cant find ref "" + referenceLootTable);
361359
file_text_close(file);
362360
return 0;
363361
}
364362
var lootStruct = variable_struct_get(data, referenceLootTable);
365363
366364
if (!variable_struct_exists(lootStruct, ""GuaranteedItems""))
367365
{
368-
scr_actionsLogUpdate(""no guaranteedItems"");
366+
scr_msl_log(""no guaranteedItems"");
369367
file_text_close(""loot_table.json"");
370368
return 0;
371369
}
372370
var guaranteedItems = variable_struct_get(lootStruct, ""GuaranteedItems"");
373371
374372
if (!scr_msl_resolve_guaranteed_items(guaranteedItems, argument1, argument0))
375373
{
376-
scr_actionsLogUpdate(""Guaranteed Items resolution failed"");
374+
scr_msl_log(""Guaranteed Items resolution failed"");
377375
file_text_close(""loot_table.json"");
378376
return 0;
379377
}
380378
381379
if (!variable_struct_exists(lootStruct, ""RandomLootMin"") || !variable_struct_exists(lootStruct, ""RandomLootMax"") || !variable_struct_exists(lootStruct, ""EmptyWeight""))
382380
{
383-
scr_actionsLogUpdate(""no int"");
381+
scr_msl_log(""no int"");
384382
file_text_close(""loot_table.json"");
385383
return 0;
386384
}
@@ -390,11 +388,11 @@ public static void InjectLootScripts()
390388
var emptyWeight = variable_struct_get(lootStruct, ""EmptyWeight"");
391389
392390
var iteration = randomLootMin + irandom(randomLootMax - randomLootMin);
393-
scr_actionsLogUpdate(""iteration "" + string(iteration));
391+
scr_msl_log(""iteration "" + string(iteration));
394392
395393
if (!variable_struct_exists(lootStruct, ""RandomItemsTable""))
396394
{
397-
scr_actionsLogUpdate(""no RandomItemsTable"");
395+
scr_msl_log(""no RandomItemsTable"");
398396
file_text_close(""loot_table.json"");
399397
return 0;
400398
}

0 commit comments

Comments
 (0)