-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.js
More file actions
203 lines (178 loc) · 5.79 KB
/
index.js
File metadata and controls
203 lines (178 loc) · 5.79 KB
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
var app = require("app"); // Module to control application life.
var fs = require("fs-extra");
var os = require("os");
var dialog = require("dialog");
var BrowserWindow = require("browser-window");
var mainWindow = null;
var initialPageLoad = false;
app.commandLine.appendSwitch("--enable-npapi");
if (process.platform == "darwin") {
var full_osx_version = require("child_process")
.execSync("sw_vers -productVersion")
.toString()
.trim()
.split(".");
var osx_release =
full_osx_version[0] == "10"
? Number(full_osx_version[1])
: Number(full_osx_version[0]) + 5; // + 5 to cause Big Sur and up to follow Catalina as they should instead of overlapping El Capitan
if (osx_release < 12) {
app.commandLine.appendSwitch("--ignore-certificate-errors");
}
}
var utilsdir = __dirname + "/../../utils";
// if running in non-packaged / development mode, this dir will be slightly different
if (process.env.npm_node_execpath) {
utilsdir = __dirname + "/build/utils";
}
function verifyUnity() {
if (os.platform() === "darwin") {
return true;
}
var dllpath =
app.getPath("appData") +
"/../LocalLow/Unity/WebPlayer/player/3.x.x/webplayer_win.dll";
if (fs.existsSync(dllpath)) {
var buff = fs.readFileSync(dllpath);
var hash = require("crypto")
.createHash("md5")
.update(buff)
.digest("hex");
if (hash == "33ffd00503b206260b0c273baf7e122e") {
return true;
}
}
return false;
}
function installUnity(callback) {
if (os.platform() === "darwin") {
callback();
return;
}
// run the installer silently
var child = require("child_process").spawn(
utilsdir + "/UnityWebPlayer.exe",
["/quiet", "/S"]
);
child.on("exit", function () {
console.log("Unity Web Player installed successfully.");
callback();
});
}
function initialSetup(firstTime) {
// Display a small window to inform the user that the app is working
setupWindow = new BrowserWindow({
width: 275,
height: 450,
resizable: false,
center: true,
frame: false,
});
setupWindow.loadUrl("file://" + __dirname + "/initialsetup.html");
installUnity(function () {
if (firstTime) {
// Copy default config
fs.copySync(
__dirname + "/defaults/config.json",
app.getPath("userData") + "/config.json"
);
}
setupWindow.destroy();
showMainWindow();
});
}
function zipCheck() {
if (os.platform() === "darwin") {
return false;
}
return app.getPath("exe").includes(os.tmpdir());
}
// Quit when all windows are closed.
app.on("window-all-closed", function () {
if (process.platform != "darwin") app.quit();
});
app.on("ready", function () {
// Check just in case the user forgot to extract the zip.
if (zipCheck()) {
errormsg =
"It has been detected that OpenATBPClient is running from the TEMP folder.\n\n" +
"Please extract the entire Client folder to a location of your choice before starting OpenATBPClient.";
dialog.showErrorBox("Error!", errormsg);
return;
}
var prefs = { 'plugins': true };
if (os.platform() == "darwin")
prefs['extra-plugin-dirs'] = [utilsdir];
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1090,
height: 776,
show: false,
"web-preferences": prefs,
});
mainWindow.setMinimumSize(640, 480);
// Check for first run
var configPath = app.getPath("userData") + "/config.json";
try {
if (!fs.existsSync(configPath)) {
console.log("Config file not found. Running initial setup.");
initialSetup(true);
} else {
if (verifyUnity()) {
showMainWindow();
} else {
installUnity(showMainWindow);
}
}
} catch (ex) {
dialog.showErrorBox(
"Error!",
"An error occurred while checking for the config. Make sure you have sufficent permissions."
);
app.quit();
}
mainWindow.on("closed", function () {
mainWindow = null;
});
});
function showMainWindow() {
var configPath = app.getPath("userData") + "/config.json";
var config = fs.readJsonSync(configPath);
console.log("Game URL:", config["game-url"]);
mainWindow.loadUrl(config["game-url"]);
// Reduces white flash when opening the program
mainWindow.webContents.on("did-finish-load", function () {
mainWindow.show();
mainWindow.webContents.executeJavaScript("OnResize();");
//mainWindow.webContents.openDevTools()
});
mainWindow.webContents.on("plugin-crashed", function () {
dialog.showErrorBox(
"Error!",
"Unity Web Player has crashed. Please re-open the application."
);
mainWindow.destroy();
app.quit();
});
mainWindow.webContents.on("will-navigate", function (evt, url) {
evt.preventDefault();
var configPath = app.getPath("userData") + "/config.json";
var config = fs.readJsonSync(configPath);
if (!url.startsWith(config["game-url"])) {
require("shell").openExternal(url);
} else {
mainWindow.loadUrl(url);
initialPageLoad = true;
}
});
mainWindow.webContents.on("did-fail-load", function () {
if (!initialPageLoad) {
dialog.showErrorBox(
"Error!",
"Could not load page. Check your Internet connection, and game-url inside config.json."
);
mainWindow.destroy();
app.quit();
}
});
}