about summary refs log tree commit diff stats
path: root/Lingo 2.asl
blob: 9c5938739c8744d3b5945e222c61168c64f36fe9 (plain) (blame)
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
// Autosplitter script for Lingo 2, by hatkirby.
//
// Massive thanks to the game developer, Brenton, for working with me to
// make this possible.

state("Lingo2") {}

startup
{
    // Relative to Livesplit.exe
    vars.logFilePath = Directory.GetCurrentDirectory() + "\\autosplitter_lingo2.log";
    vars.log = (Action<string>)((string logLine) => {
        print("[Lingo 2 ASL] " + logLine);
        string time = System.DateTime.Now.ToString("dd/MM/yy hh:mm:ss.fff");
        // AppendAllText will create the file if it doesn't exist.
        System.IO.File.AppendAllText(vars.logFilePath, time + ": " + logLine + "\r\n");
    });

    settings.Add("panels", false, "Split on solving a panel");
    settings.Add("panels_first", false, "Only split the first time each panel is solved", "panels");
    settings.Add("maps", false, "Split on changing maps");
    settings.Add("maps_first", false, "Only split the first time a map is entered", "maps");
    settings.Add("ends", false, "Split on any ending");
    settings.Add("keys", false, "Split on unlocking a key");
    settings.Add("paintings", false, "Split on unlocking a gallery painting");
    settings.Add("graves", false, "Split on completing a gravestone");
    settings.Add("masteries", false, "Split on collecting a mastery");

    vars.prevPanelMap = "";
    vars.prevPanelPath = "";
    vars.prevMap = "";
    vars.collectedKeys = new HashSet<string>();
    vars.latestKeyKey = null;

    vars.log("Autosplitter loaded");
}

init
{
    // magic byte array format:
    // [0-7]: 9c 46 9f b0 4b 6a e0 8d (random bytes used for sigscanning)
    // [8]: Loading flag
    // [9]: First movement flag
    // [10]: Latest unlocked key (0 if run just started)
    // [11]: 1 or 2, indicating the level of the latest unlocked key
    // [12-52]: Latest collectable unlocked (null terminated, truncated at 40 chars excluding null)
    // [53-93]: Current map name (null terminated, truncated at 40 chars excluding null)
    // [94-134]: Map name of latest solved panel (null terminated, truncated at 40 chars excluding null)
    // [135-235]: Full nodepath of latest solved panel (null terminated, truncated at 100 chars excluding null)
    IntPtr ptr = IntPtr.Zero;
    foreach (var page in game.MemoryPages(true).Reverse()) {
        var scanner = new SignatureScanner(game, page.BaseAddress, (int)page.RegionSize);
        ptr = scanner.Scan(new SigScanTarget(0, "9c 46 9f b0 4b 6a e0 8d"));
        if (ptr != IntPtr.Zero) {
            break;
        }
    }
    if (ptr == IntPtr.Zero) {
        throw new Exception("Could not find magic autosplitter array!");
    }
    vars.loading = new MemoryWatcher<byte>(ptr + 8);
    vars.firstInput = new MemoryWatcher<byte>(ptr + 9);
    vars.latestKey = new MemoryWatcher<byte>(ptr + 10);
    vars.latestKeyLevel = new MemoryWatcher<byte>(ptr + 11);
    vars.latestCollectible = new StringWatcher(ptr + 12, 41);
    vars.currentMap = new StringWatcher(ptr + 53, 41);
    vars.lastPanelMap = new StringWatcher(ptr + 94, 41);
    vars.lastPanelPath = new StringWatcher(ptr + 135, 101);

    vars.log(String.Format("Magic autosplitter array: {0}", ptr.ToString("X")));
}

update
{
    vars.loading.Update(game);
    vars.firstInput.Update(game);
    vars.latestKey.Update(game);
    vars.latestKeyLevel.Update(game);
    vars.latestCollectible.Update(game);
    vars.currentMap.Update(game);
    vars.lastPanelMap.Update(game);
    vars.lastPanelPath.Update(game);

    if (vars.latestKeyLevel.Current > 0)
    {
        vars.latestKeyKey = string.Format("{0}{1}", (char)vars.latestKey.Current, vars.latestKeyLevel.Current);
    }
}

start
{
    return vars.firstInput.Old == 0 && vars.firstInput.Current == 1;
}

onStart
{
    vars.prevPanelMap = vars.lastPanelMap.Current;
    vars.prevPanelPath = vars.lastPanelPath.Current;
    vars.prevMap = vars.currentMap.Current;
}

reset
{
    return vars.firstInput.Old == 1 && vars.firstInput.Current == 0;
}

isLoading
{
    return vars.loading.Current == 1;
}

split
{
    bool should_split = false;

    if (vars.lastPanelMap.Current != vars.prevPanelMap || vars.lastPanelPath.Current != vars.prevPanelPath) {
        if (settings["panels"]) {
            should_split = true;
            vars.log("Split on any panel: " + vars.lastPanelMap.Current + " / " + vars.lastPanelPath.Current);
        }

        vars.prevPanelMap = vars.lastPanelMap.Current;
        vars.prevPanelPath = vars.lastPanelPath.Current;
    }
    else if (vars.currentMap.Current != vars.prevMap)
    {
        if (settings["maps"]) {
            should_split = true;
            vars.log("Split on map change: " + vars.currentMap.Current);
        }

        vars.prevMap = vars.currentMap.Current;
    }
    else if (vars.latestKeyKey != null && !vars.collectedKeys.Contains(vars.latestKeyKey))
    {
        if (settings["keys"]) {
            should_split = true;
            vars.log("Split on collected key: " + vars.latestKeyKey);
        }
        
        vars.collectedKeys.Add(vars.latestKeyKey);
    }

    return should_split;
}