Skip to content

Instantly share code, notes, and snippets.

@Lili1228
Created October 26, 2023 14:19
Show Gist options
  • Save Lili1228/df933a33333c9271e391d61ed695816e to your computer and use it in GitHub Desktop.
Save Lili1228/df933a33333c9271e391d61ed695816e to your computer and use it in GitHub Desktop.
Subtract time from ETS2/ATS save
def subtract_time(threshold_mins: int, next: int, offset: int = 0) -> None:
'''
Subtracts time in a Euro Truck Simulator 2 (and possibly American Truck Simulator) save file.
I wrote this because TruckersMP, before 2021-08-10 (https://forum.truckersmp.com/?/topic/105649--/),
save time was synced with server time, bloating it by years (about 25 in my case).
Save must be first not on Steam Cloud and be decrypted, see:
https://forum.truckersmp.com/index.php?/topic/44399-save-editing-tutorial-in-depth-video/
Function assumes it's run in the directory with game.sii and info.sii. The time is stored in multiple places, I used
this function looking at delivery_log_entry blocks. Quick walkthrough for the relevant parts of the block:
param[0] is when the entry was finished
param[1] is where the delivery was started
param[2] is where the delivery was finished
param[15] is when an off-line delivery was started, same as param[0] otherwise
param[18] is the type of the event. Old entries don't have it at all, but if it exists:
- compn is a standard off-line delivery with your truck, it's the only one with the start time
- on_compn is a World of Trucks delivery
- freerm is the EXP points given by riding in free roam
param[0] and param[15] are given in minutes since day 1 at midnight and one day is 1,440 minutes. If there's a hole
of thousands or even millions of minutes between the param[0] in one entry and param[15] in the next one, it's
likely TruckersMP's fault.
Since some of the timestamps are in days, this function subtracts only days (or multiples of 1,440 minutes)
from timestamps after given time. Some entries in my save were going back in time before launching this function,
it still works fine.
New save is written as save_new.sii and info_new.sii
Tested on 1.48.5 on Windows.
:param int threshold_mins: from what timestamp onwards to subtract time, rounded up to the next midnight, I used
the last params[0]
:param int next: first timestamp in save after threshold_mins, I used the next params[15]
:param offset: by how many more days more to subtract the timestamps, use for fine tuning
:type offset: int or None
'''
interesting_mins = ('params[0]', 'params[15]', 'game_time', 'last_visit', 'expiration_time', 'time', 'expiration')
interesting_days = ('timestamp_day', 'economy_day')
threshold_days = threshold_mins // 1440 + 1
threshold_mins = threshold_days * 1440
how_many_days = next // 1440 - threshold_days + offset
how_many_mins = how_many_days * 1440
o = open('game_new.sii', 'w')
with open('game.sii', 'r') as i:
for line in i:
temp = line.split(':')
if temp[0][1:] in interesting_mins + interesting_days:
try:
data = int(temp[1].split()[0])
except ValueError: # it can be nil
o.write(line)
continue
if temp[0][1:] in interesting_mins:
if data >= threshold_mins:
data -= how_many_mins
line = temp[0] + ': ' + str(data) + '\n'
elif data >= threshold_days:
data -= how_many_days
line = temp[0] + ': ' + str(data) + '\n'
o.write(line)
o.close()
o = open('info_new.sii', 'w')
with open('info.sii', 'r') as i:
for line in i:
if line.startswith(' time: '):
o.write(' time: ' + str(int(line.split(':')[1]) - how_many_mins) + '\n')
else:
o.write(line)
o.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment