-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeague-Server.lua
622 lines (592 loc) · 20 KB
/
League-Server.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
--League-Server by GRAND(Gerlamp) and Mohamed Amr Nady
--Research: SMc81, .andre92
--Special thank's: crabshank
local m = { version = 1.2 }
-- Constants
local contentPath = ".\\content\\League-Server\\"
local startAddress = 0x00007FF4D0000000
local endAddress = 0x00007FF4EFFFFFF0
local hexPat = "0x%x+"
local BlankDate = "\xff\xff\xff\xff\x37\x00\x00\x00\x03\x00\x00\x00\xff\xff\xff\xff"
local _empty = {}
-- Variables
local compsMap
local mlteamnow = {}
local CalendarAddresses = {}
local Schedule = {}
local teamNamestoHex = {}
local customMatchdaysData = {}
-- Config
local isDebugging = false
-- local changeDate = false
function m.dispose()
mlteamnow = {}
CalendarAddresses = {}
Schedule = {}
teamNamestoHex = {}
customMatchdaysData = {}
collectgarbage()
end
local function get_rlm_lib(ctx)
return ctx.real_life_mode or _empty
end
local function date_to_totaldays(date)
local days_in_each_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
local i, j = date:match("(%d+)/(%d+)")
local month, day = tonumber(i), tonumber(j)
if month > 0 and month <= 12 and day > 0 and day <= 31 then
if day > days_in_each_month[month] then
log("Incorrect Date: day should be smaller")
return nil
end
local totaldays = day
for i = 1, month - 1 do
totaldays = totaldays + days_in_each_month[i]
end
return totaldays
else
log("Incorrect Date, setting to 0...")
return 0
end
end
local function teamHextoID(teamHex)
function getBits(num)
local x = {}
while num > 0 do
rest = num % 2
table.insert(x, 1, rest)
num = (num - rest) / 2
end
return table.concat(x) or x
end
function binToNum(binary)
local bin, sum = tostring(binary):reverse(), 0
for i = 1, #bin do
num = bin:sub(i, i) == "1" and 1 or 0
sum = sum + num * 2 ^ (i - 1)
end
return sum
end
return binToNum(getBits(memory.unpack("u32", teamHex)):sub(1, -15))
end
local function gamedayToTeamIDs(rlmLib, total_games_per_matchday, id, type, isGeneric)
local t = {}
for n = 1, total_games_per_matchday do
local gameAddress = rlmLib.get_comp_schedule(id, type, isGeneric, 1, n, total_games_per_matchday)
local homeHex = memory.read(gameAddress + 20, 4)
local awayHex = memory.read(gameAddress + 24, 4)
t[teamHextoID(homeHex)] = homeHex
t[teamHextoID(awayHex)] = awayHex
log(string.format("%d is done", n))
end
return t
end
local function tableToTeamIDs(table)
local t = {}
for n = 1, #table do
t[table[n].dec] = table[n].hex
log(string.format("plz %d %s", table[n].dec, memory.hex(table[n].hex)))
end
return t
end
local function mapTeamIDs(teamIDs, namesMap)
local t = {}
-- loop 1: match available ids and leave unavailable untouched
for name, id in pairs(namesMap) do
if teamIDs[id] then -- available
log(string.format("found %s id: %d", name, id))
t[name] = teamIDs[id]
teamIDs[id] = nil
namesMap[name] = nil
end
end
-- loop 2: match unavailable ids
for name, id in pairs(namesMap) do
if id then -- unavailable
for newId, hex in pairs(teamIDs) do
if hex then
log(string.format("matched %s old: %d new: %d", name, id, newId))
t[name] = hex
teamIDs[newId] = nil
break
end
end
end
end
return t
end
local function getAddressWithVariableBytes(addrBeginning, variableByteLength, addrEndning, variableStartAddress)
local addr = memory.safe_search(addrEndning, variableStartAddress, endAddress)
if addr then
if memory.read(addr - #addrBeginning - variableByteLength, #addrBeginning) == addrBeginning then
return addr - variableByteLength - #addrBeginning
else
return getAddressWithVariableBytes(addrBeginning, variableByteLength, addrEndning, addr + #addrEndning)
end
else
return nil
end
end
local function getGamesOfLeagueUsingMemory(currentleagueid, matchdaytotal)
local variableBytesLength = 20
local addrEndning = "\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF"
local returnValueOffset = -2
local t = {}
for i = 1, matchdaytotal do
local all_found = false
local variableStartAddress = startAddress
local addrBeginning = "\x00\x00" .. currentleagueid .. memory.pack("u8", i - 1) .. "\x20"
log(string.format("searching matchday %d", i))
t[i] = {}
while all_found == false and variableStartAddress <= endAddress do
local addr = memory.safe_search(addrBeginning, variableStartAddress, endAddress)
if addr then
if memory.read(addr + #addrBeginning + variableBytesLength, #addrEndning) == addrEndning then
log("address matches criteria, inserting...")
table.insert(t[i], addr + returnValueOffset)
else
log("address doesn't matches criteria, skipping...")
end
variableStartAddress = tonumber(string.match(tostring(addr), hexPat)) + 1
else
all_found = true
log(string.format("found %d address in matchday %d", #t[i], i))
end
end
end
return t
end
-- local function getAllAddressesWithVariableBytes(addrBeginning, variableByteLength, addrEndning, variableStartAddress,
-- returnValueOffset)
-- local t = {}
-- local all_found = false
-- while all_found == false and variableStartAddress <= endAddress do
-- local addr = memory.safe_search(addrBeginning, variableStartAddress, endAddress)
-- if addr == nil then
-- all_found = true
-- log(string.format("found %d address", #t))
-- else
-- local ad = tonumber(string.match(tostring(addr), hexPat))
-- if type(ad) == 'number' then
-- log("found address")
-- if memory.read(addr + #addrBeginning + variableByteLength, #addrEndning) == addrEndning then
-- log("address matches criteria, inserting...")
-- log(memory.hex(memory.read(ad + returnValueOffset, #addrBeginning)))
-- table.insert(t, ad + returnValueOffset)
-- -- else
-- -- log(memory.hex(addr))
-- end
-- variableStartAddress = ad + #addrBeginning
-- end
-- end
-- end
-- return t
-- end
local function tableIsEmpty(self)
for _, _ in pairs(self) do
return false
end
return true
end
local function writeGame(
rlmLib,
config,
startingYear,
fixtureNumber,
gameweekNumber,
from_total_days,
to_total_days,
to_month,
to_day,
isNight,
matchStartTime,
homeTeam,
awayTeam,
isGeneric,
isCurrentTeamInLeague,
fixtureNumberInterval
)
local fixNoHex = memory.pack(
"u16",
fixtureNumber - 1 + config.TOTAL_GAMES_PER_MATCHDAY * (gameweekNumber - 1) + fixtureNumberInterval
)
-- Matchday Writing
-- E6 00 (Fixture id)
-- 00 00 11 00 (Liga id)
-- 17 (Matchday)
-- 20 (League type)
-- E5 07 01 1A (Date 2021/01/26)
-- 00 00 00 00 (Night Boolean)
-- 10 00 00 00 (Match time)
-- 10 C0 2C 00 (Home team)
-- 04 80 19 00 (Away team)
local gameAddress = rlmLib.get_comp_schedule(
config.ID,
config.TYPE,
isGeneric,
gameweekNumber,
fixtureNumber,
config.TOTAL_GAMES_PER_MATCHDAY
)
local matchdaySchedule = rlmLib.get_schedule_matchday(config.ID, config.TYPE, gameweekNumber, fixtureNumber)
-- we need to know the from date, unless its generic (/xff/xff)
if from_total_days == 0 and not isGeneric then
-- memory.write(
-- gameAddress + 8,
-- memory.pack("u16", startingYear) .. memory.pack("u8", to_month) .. memory.pack("u8", to_day)
-- )
from_total_days = date_to_totaldays(
string.format(
"%02d/%02d",
memory.unpack("u8", memory.read(gameAddress + 10, 1)),
memory.unpack("u8", memory.read(gameAddress + 11, 1))
)
)
end
if isDebugging then
log(
string.format(
"matchdays: Game %d of MatchDay %d Before: %s",
fixtureNumber,
gameweekNumber,
memory.hex(memory.read(gameAddress, 28))
)
)
log(
string.format(
"gamesSchedule: Game %d of MatchDay %d Before: %s",
fixtureNumber,
gameweekNumber,
memory.hex(memory.read(matchdaySchedule, 18))
)
)
end
if isNight then
memory.write(gameAddress + 12, memory.pack("u16", isNight))
end
if matchStartTime then
memory.write(gameAddress + 16, memory.pack("u16", matchStartTime))
end
if homeTeam then
memory.write(gameAddress + 20, teamNamestoHex[homeTeam])
memory.write(matchdaySchedule + 10, teamNamestoHex[homeTeam])
end
if awayTeam then
memory.write(gameAddress + 24, teamNamestoHex[awayTeam])
memory.write(matchdaySchedule + 14, teamNamestoHex[awayTeam])
end
if to_total_days and to_month and to_day then
-- calc sum of games on to date
local sum = memory.unpack("u8", memory.read(Schedule[to_total_days], 1)) + 1
-- change to_date on gameAddress
memory.write(
gameAddress + 8,
memory.pack("u16", startingYear) .. memory.pack("u8", to_month) .. memory.pack("u8", to_day)
)
-- Schedule Writing
-- update sum
memory.write(Schedule[to_total_days], memory.pack("u8", sum))
-- Write new fixture after previous ones (if there any)
memory.write(Schedule[to_total_days] - (sum * -2 + 562), fixNoHex)
-- Remove fixture from Schedule
if not isGeneric and from_total_days ~= 0 then
local fromFixNo = memory.read("u16", CalendarAddresses[from_total_days], 2)
for i = 1, 560 do
local fromFixTempo = memory.read("u8", Schedule[from_total_days] + (i - 560))
if fromFixTempo == fromFixNo then
memory.write(Schedule[from_total_days] + (i - 560), "\xff")
break
end
end
end
-- Stop or Skip
if isCurrentTeamInLeague then
if mlteamnow.hex == teamNamestoHex[homeTeam] or mlteamnow.hex == teamNamestoHex[awayTeam] then
log("currunt match has current team")
-- Stop
memory.write(Schedule[to_total_days] + 7, "\x00")
-- Calendar Writing
-- TODO: Define that for UCL,... (01), domestic league/cup (02), or nothing (03)
local game_type_hex = "\x02\x00"
memory.write(CalendarAddresses[to_total_days], fixNoHex) -- C8 00 Matchday ID
memory.write(CalendarAddresses[to_total_days] + 2, config["ID"].hex) -- 11 00 League ID
memory.write(CalendarAddresses[to_total_days] + 4, memory.pack("u16", gameweekNumber - 1)) -- 14 00 Matchday № this 21 day
memory.write(CalendarAddresses[to_total_days] + 6, "\x00\x00") -- 00 00 Blank
memory.write(CalendarAddresses[to_total_days] + 8, game_type_hex) -- 02 00 Playable day (01 UCL) (03 not Playableday)
memory.write(CalendarAddresses[to_total_days] + 10, "\x00\x00") -- 00 00 Blank
memory.write(CalendarAddresses[to_total_days] + 12, mlteamnow.hex)
if not isGeneric and from_total_days ~= 0 and from_total_days ~= to_total_days then
memory.write(CalendarAddresses[from_total_days], BlankDate) -- (from,Blank)
end -- Team ID
else
-- Skip
-- That might be causing an error that's we didn't experience yet
-- Some sort of double writing
memory.write(Schedule[to_total_days] + 7, "\xff")
-- Calendar Writing
end
end
end
if isDebugging then
log(
string.format(
"matchdays: Game %d of MatchDay %d After: %s",
fixtureNumber,
gameweekNumber,
memory.hex(memory.read(gameAddress, 28))
)
)
log(
string.format(
"gamesSchedule: Game %d of MatchDay %d After: %s",
fixtureNumber,
gameweekNumber,
memory.hex(memory.read(matchdaySchedule, 18))
)
)
end
end
function m.data_ready(ctx, filename)
--Main
local newml = string.match(filename, "common\\demo\\fixdemo\\mode\\cut_data\\mode_meeting_reply_0%d%_pl.fdc")
local newbl = string.match(filename, "common\\demo\\fixdemo\\mode\\cut_data\\mode_firstday_BL01_1_pl.fdc")
local midseason =
string.match(filename, "common\\demo\\fixdemo\\mode\\cut_data\\mode_meeting_report_01_stand_cam_B.fdc")
--local newml = string.match(filename, "common\\demo\\fixdemo\\mode\\cut_data\\mode_meeting_mission_07a_manager.fdc")
local loadml = string.match(filename, "common\\script\\flow\\ML\\MLCoachCapturePostLoad.json") --common\\script\\flow\\ML\\MLMainManu.json
local updateday = string.match(filename, "common\\script\\flow\\ML\\MLMainManu.json") -- common\\script\\flow\\Common\\CmnScheduleProcess.json
local rlmLib = get_rlm_lib(ctx)
local year
if updateday then
year = rlmLib.hook_year()
currentMonth = memory.unpack("u8", memory.read(year + 7, 1))
currentDay = memory.unpack("u8", memory.read(year + 8, 1))
midjan = (currentMonth == 7 and currentDay == 1)
midjune = (currentMonth == 1 and currentDay == 1)
log(string.format("currentMonth: %d", currentMonth))
log(string.format("currentDay: %d", currentDay))
end
if newml or newbl then --or (midjune and updateday) or (midjan and updateday)
year = rlmLib.hook_year()
log("**Writing Started**")
local pandas = ctx.external_files
local position = memory.read(year - 3, 1)
local yearnow = rlmLib.current_season()
-- startingYear = yearnow.dec
-- endingYear = yearnow.dec + 1
local datenow = memory.read(year + 5, 4)
local currentMonth = memory.unpack("u8", memory.read(year + 7, 1))
-- local currentleagueid = memory.read(year + 51, 1)
mlteamnow = rlmLib.current_team_id()
local currentleagueid = {}
currentleagueid.dec =
ctx.common_lib.compID_to_tournamentID_map[ctx.common_lib.leagues_of_teams[mlteamnow.dec][1]]
currentleagueid.hex = memory.pack("u16", currentleagueid.dec)
log(string.format("ML Team: %d", mlteamnow.dec))
log(string.format("League ID: %d", currentleagueid.dec))
log(string.format("Season: %d", yearnow.dec))
if not tableIsEmpty(compsMap) then
local leagues_configs = {}
for i, compName in pairs(compsMap) do -- Load all league configs to return current league config easily
leagues_configs[i] = pandas.read_ini(contentPath .. string.format("\\%s\\", compName) .. "config.ini")
if leagues_configs[i] ~= nil then
leagues_configs[i]["NAME"] = compName
leagues_configs[i]["ID"] = {}
leagues_configs[i]["ID"].dec = i
leagues_configs[i]["ID"].hex = memory.pack("u16", i)
else
table.remove(leagues_configs)
end
end
-- local compName = compsMap[currentleagueid.dec]
for i, config in pairs(leagues_configs) do
local configPath = contentPath .. string.format("\\%s\\", config["NAME"])
local existingYears = pandas.read_text(configPath .. "\\years.txt")
if tableIsEmpty(CalendarAddresses) then
for counter = 1, 365 do
if isDebugging then
log(
string.format(
"Calendar: Day %d : %s",
counter,
memory.hex(memory.read(year + counter * 16 + 1, 16))
)
)
end
table.insert(CalendarAddresses, counter, year + counter * 16 + 1)
end
end
-- Schedule -- Found in ML Main Menu Bottom Left and Center -- Needs optimization
if tableIsEmpty(Schedule) then
local addr
if leagues_configs[currentleagueid.dec]["STARTS_IN_JAN"] == "true" then
addr = getAddressWithVariableBytes(
"\x00\x00\x03\x00\x00\x00",
2,
"\x01\x01\x01\x00\x01",
startAddress
)
elseif leagues_configs[currentleagueid.dec]["STARTS_IN_JAN"] == "false" then
addr = getAddressWithVariableBytes(
"\x00\x00\x06\x00\x00\x00",
2,
"\x01\x01\x09\x00\xF5",
startAddress
)
else
error("STARTS_IN_JAN field in config.ini is mandatory")
end
if addr then
table.insert(Schedule, 1, addr - 2)
for day = 1, 364 do -- day 0 is already there
table.insert(Schedule, day + 1, Schedule[1] + 708 * day)
if isDebugging then
log(
string.format(
"Schedule: Day %d : %s",
day + 1,
memory.hex(memory.read(Schedule[1] + 708 * day, 15))
)
)
end
end
else
error("schecule was not found, aborting...")
end
end
if
(currentMonth == 1 and config["STARTS_IN_JAN"] == "true")
or ((currentMonth >= 6 and currentMonth <= 8) and config["STARTS_IN_JAN"] == "false")
then
local fixtureNumberInterval
if config["TOTAL_MATCHDAYS"] == nil then
config["TOTAL_MATCHDAYS"] = config["TOTAL_TEAMS"] * 2 - 2
end
if config["TOTAL_GAMES_PER_MATCHDAY"] == nil then
config["TOTAL_GAMES_PER_MATCHDAY"] = config["TOTAL_TEAMS"] / 2
end
local hasCustom = existingYears[tostring(yearnow.dec)] or config["DEFAULT_MAP"]
local isGeneric = config["IS_GENERIC"] == "true"
if true then -- a scope, i don't need addr out of here
local addr = rlmLib.get_comp_schedule(
config.ID,
config.TYPE,
isGeneric,
1,
1,
config.TOTAL_GAMES_PER_MATCHDAY
)
if addr then
fixtureNumberInterval = memory.unpack("u16", memory.read(addr, 2))
end
end
-- intialize the addresses needed
if fixtureNumberInterval or not rlmLib.get_schedule_matchday(config.ID, config.TYPE, 1, 1) then
if hasCustom then -- custom edit based on year
local mapsPath = configPath .. hasCustom
customMatchdaysData = pandas.read_csv(mapsPath .. "\\map_matchdays.csv")
teamNamestoHex = mapTeamIDs(
gamedayToTeamIDs(
rlmLib,
config.TOTAL_GAMES_PER_MATCHDAY,
config.ID,
config.TYPE,
isGeneric
),
-- tableToTeamIDs(rlmLib.comp_table(config["ID"], config["TOTAL_TEAMS"], "current")),
pandas.read_num_text_map(mapsPath .. "\\map_team.csv")
)
for n = 1, #customMatchdaysData do
local from_total_days
if not isGeneric then
if
customMatchdaysData[n]["FromDate"]
and customMatchdaysData[n]["FromDate"] ~= "00/00"
then
from_total_days = date_to_totaldays(customMatchdaysData[n]["FromDate"])
else
from_total_days = 0
end
end
local to_month, to_day
local to_total_days
if customMatchdaysData[n]["ToDate"] then
local mon, day = customMatchdaysData[n]["ToDate"]:match("(%d+)/(%d+)")
to_month, to_day = tonumber(mon), tonumber(day)
to_total_days = date_to_totaldays(customMatchdaysData[n]["ToDate"])
end
-- fixture and gameweek number is a must
local fixtureNumber = tonumber(customMatchdaysData[n]["FixtureNumber"])
local gameweekNumber = tonumber(customMatchdaysData[n]["CompetitionGameweek"])
local isNight
if customMatchdaysData[n]["isNight"] then
isNight = tonumber(customMatchdaysData[n]["isNight"])
end
local matchStartTime
if customMatchdaysData[n]["Time"] then
matchStartTime = tonumber(customMatchdaysData[n]["Time"])
end
local homeTeam
if customMatchdaysData[n]["HomeT"] then
homeTeam = customMatchdaysData[n]["HomeT"]
end
local awayTeam
if customMatchdaysData[n]["AwayT"] then
awayTeam = customMatchdaysData[n]["AwayT"]
end
local startingYear = yearnow.dec
if config["STARTS_IN_JAN"] == "false" then
if to_month <= 6 then
startingYear = yearnow.dec + 1
end
end
writeGame(
rlmLib,
config,
startingYear,
fixtureNumber,
gameweekNumber,
from_total_days,
to_total_days,
to_month,
to_day,
isNight,
matchStartTime,
homeTeam,
awayTeam,
isGeneric,
i == currentleagueid.dec,
fixtureNumberInterval
)
end
else
log("current year config is not found, aborting...")
end
else
log(string.format("skipping %s, addresses weren't found", config["NAME"]))
end
else
log(string.format("skipping %s, league starts in jan? %s", config["NAME"], config["STARTS_IN_JAN"]))
end
-- else
-- log("league is not in content folder, nothing has changed")
-- log("if that should not happen, make sure the league name matches the map")
-- log("and it has config.ini in it with required parameters")
end
m.dispose()
else
log("that module is useless")
log(string.format("since %s is empty or problem in map", contentPath))
log("disable it for better experience")
end
end
end
function m.init(ctx)
if contentPath:sub(1, 1) == "." then
contentPath = ctx.sider_dir .. contentPath
end
m.dispose()
compsMap = ctx.external_files.read_text_num_map(contentPath .. "\\map_competitions.txt")
ctx.register("livecpk_data_ready", m.data_ready)
end
return m