aboutsummaryrefslogtreecommitdiff
path: root/src-ui/src/main.ts
blob: ba45b20ce136b224f30228b1bbb8cce91bb955f9 (plain)
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
import { invoke } from "@tauri-apps/api";
import { listen, Event as TauriEvent } from "@tauri-apps/api/event";
import { open } from '@tauri-apps/api/dialog';
import { appDir } from '@tauri-apps/api/path';

const $ = document.querySelector.bind(document);
const button_install_string = "Install Northstar";
const button_in_install_string = "Installing...";
const button_update_string = "Update Northstar";
const button_in_update_string = "Updating...";
const button_play_string = "Launch Northstar";
const button_launched_string = "Launched Northstar";
const button_manual_find_string = "Manually select Titanfall2 install location";

interface GameInstall {
    game_path: string;
    install_type: string;
}

// Stores the overall state of the application
var globalState = {
    gamepath: "",
    installed_northstar_version: "",
    northstar_package_name: "Northstar",
    current_view: "" // Note sure if this is the right way to do it
}

async function get_northstar_version_number_and_set_button_accordingly(omniButtonEl: HTMLElement) {
    let northstar_version_number = await invoke("get_northstar_version_number_caller", { gamePath: globalState.gamepath }) as string;
    if (northstar_version_number && northstar_version_number.length > 0) {
        globalState.installed_northstar_version = northstar_version_number;

        // Show installed Northstar version
        let northstarVersionHolderEl = $("northstar-version-holder") as HTMLElement;
        northstarVersionHolderEl.textContent = `Installed Northstar version: v${globalState.installed_northstar_version}`;

        omniButtonEl.textContent = button_play_string;
        await invoke("check_is_northstar_outdated", { gamePath: globalState.gamepath, northstarPackageName: globalState.northstar_package_name })
            .then((message) => {
                console.log(message);
                if (message) {
                    omniButtonEl.textContent = button_update_string;
                }
            })
            .catch((error) => {
                console.error(error);
                alert(error);
            });
    }
}

async function manually_find_titanfall2_install(omniButtonEl: HTMLElement) {
    // Open a selection dialog for directories
    const selected = await open({
        directory: true,
        multiple: false,
        defaultPath: await appDir(),
    });
    if (Array.isArray(selected)) {
        // user selected multiple directories
        alert("Please only select a single directory");
    } else if (selected === null) {
        // user cancelled the selection
    } else {
        // user selected a single directory

        // Verify if valid Titanfall2 install location
        let is_valid_titanfall2_install = await invoke("verify_install_location", { gamePath: selected }) as boolean;
        if (is_valid_titanfall2_install) {
            globalState.gamepath = selected;

            let installLocationHolderEl = document.getElementById("install-location-holder") as HTMLInputElement;
            installLocationHolderEl.value = globalState.gamepath;

            // Update omni-button
            omniButtonEl.textContent = button_install_string;

            // Check for Northstar install
            await get_northstar_version_number_and_set_button_accordingly(omniButtonEl);
        }
        else {
            // Not valid Titanfall2 install
            alert("Not a valid Titanfall2 install");
        }
    }
}

document.addEventListener("DOMContentLoaded", async function () {
    // get the elements
    // const helloEl = $("div.hello")! as HTMLElement;
    // let counterButtonEl = $("counter-button") as HTMLElement;
    // let counterResultEl = $("counter-result") as HTMLElement;
    let pingEl = $("backend-ping")! as HTMLElement;
    let panicButtonEl = $("panic-button") as HTMLElement;
    let installLocationHolderEl = document.getElementById("install-location-holder") as HTMLInputElement;
    let versionNumberHolderEl = $("version-number-holder") as HTMLElement;
    let installTypeHolderEl = $("install-type-holder") as HTMLElement;
    let omniButtonEl = document.getElementById("omni-button") as HTMLElement;
    let originRunningHolderEl = $("origin-running-holder") as HTMLElement;
    let northstarVersionHolderEl = $("northstar-version-holder") as HTMLElement;
    let useReleaseCandidateCheckboxEl = document.getElementById("use-release-candidate-checkbox") as HTMLInputElement;

    useReleaseCandidateCheckboxEl.addEventListener('change', function () {
        // Switch between main release and release candidates
        if (this.checked) {
            globalState.northstar_package_name = "NorthstarReleaseCandidate"
        } else {
            globalState.northstar_package_name = "Northstar";
        }
        // Update the button
        get_northstar_version_number_and_set_button_accordingly(omniButtonEl);
    });

    // listen backend-ping event (from Tauri Rust App)
    listen("backend-ping", function (evt: TauriEvent<any>) {
        pingEl.classList.add("on");
        setTimeout(function () {
            pingEl.classList.remove("on");
        }, 500);
    })

    // listen origin-running-ping event (from Tauri Rust App)
    listen("origin-running-ping", function (evt: TauriEvent<any>) {
        let origin_is_running = evt.payload as boolean;
        if (origin_is_running) {
            originRunningHolderEl.textContent = "ORIGIN RUNNING";
        }
        else {
            originRunningHolderEl.textContent = "ORIGIN NOT RUNNING";
        }
        console.log(evt.payload);
    })

    // omni button click
    omniButtonEl.addEventListener("click", async function () {

        switch (omniButtonEl.textContent) {

            // Find Titanfall2 install manually
            case button_manual_find_string:
                manually_find_titanfall2_install(omniButtonEl);
                break;

            // Install Northstar
            case button_install_string:
                let install_northstar_result = invoke("install_northstar_caller", { gamePath: globalState.gamepath, northstarPackageName: globalState.northstar_package_name });

                // Update button while installl process is run
                omniButtonEl.textContent = button_in_install_string;

                await install_northstar_result.then((message) => {
                    console.log(message);
                })
                    .catch((error) => {
                        console.error(error);
                        alert(error);
                    });

                get_northstar_version_number_and_set_button_accordingly(omniButtonEl);
                break;

            // Update Northstar
            case button_update_string:
                let update_northstar_result = invoke("update_northstar_caller", { gamePath: globalState.gamepath, northstarPackageName: globalState.northstar_package_name });

                // Update button while update process is run
                omniButtonEl.textContent = button_in_update_string;

                await update_northstar_result.then((message) => {
                    console.log(message);
                })
                    .catch((error) => {
                        console.error(error);
                        alert(error);
                    });

                // Update button to display new version
                get_northstar_version_number_and_set_button_accordingly(omniButtonEl);
                break;

            // Launch Northstar
            case button_play_string:
                let game_install = {
                    game_path: globalState.gamepath,
                    install_type: installTypeHolderEl.textContent
                } as GameInstall;
                await invoke("launch_northstar_caller", { gameInstall: game_install })
                    .then((message) => {
                        console.log(message);
                        omniButtonEl.textContent = button_launched_string;
                    })
                    .catch((error) => {
                        console.error(error);
                        alert(error);
                    });
                break;

            // Do nothing when clicked during install/update/game-launched
            case button_in_update_string:
            case button_in_install_string:
            case button_launched_string:
                break;

            // Fallback
            default:
                alert(`Not implemented yet: ${omniButtonEl.textContent}`);
                break;
        }
    });

    // // counter button click
    // counterButtonEl.addEventListener("pointerup", async function () {
    //     const result = await invoke("add_count", { num: 1 }) as string;
    //     counterResultEl.textContent = result;
    // });

    // // hello click
    // helloEl.addEventListener("pointerup", async function () {
    //     const result = await invoke("hello_world") as string;
    //     helloEl.textContent = result;
    //     setTimeout(function () {
    //         helloEl.textContent = "Click again";
    //     }, 1000);
    // })

    // panic button click
    panicButtonEl.addEventListener("pointerup", async function () {
        await invoke("force_panic");
        alert("Never should have been able to get here!");
    });

    // Run the following on initial page load
    // Get version number
    let version_number_string = await invoke("get_version_number") as string;
    // Get host OS
    let host_os_string = await invoke("get_host_os_caller") as string;
    versionNumberHolderEl.textContent = `${version_number_string} (${host_os_string})`;

    // Get install location
    await invoke("find_game_install_location_caller", { gamePath: globalState.gamepath })
        .then((game_install) => {
            // Found some gamepath

            console.log(game_install);

            let game_install_obj = game_install as GameInstall;

            // Change omni-button content based on whether game install was found
            omniButtonEl.textContent = button_install_string;

            installLocationHolderEl.value = game_install_obj.game_path;
            globalState.gamepath = game_install_obj.game_path;

            installTypeHolderEl.textContent = game_install_obj.install_type;

            // Check installed Northstar version if found
            get_northstar_version_number_and_set_button_accordingly(omniButtonEl);
            console.log(globalState);

        })
        .catch((error) => {
            // Gamepath not found or other error
            console.error(error);
            alert(error);
            omniButtonEl.textContent = button_manual_find_string;
        });
})