-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
216 lines (195 loc) · 7.97 KB
/
main.py
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
import os
import struct
import sys
import time
import Utils
import json
import configparser
import winsound
from Commands import commands
def GetBasePath():
return os.path.abspath(".")
def GetAbsolutePath(relative_path):
return os.path.join(GetBasePath(), relative_path)
AccreditationLevel = 0
Registry = configparser.ConfigParser()
AdriveExists = True
OSVersion = ""
OSRegistryPath = GetAbsolutePath('./OSRegistry.ini')
def LoadOS():
Utils.OSClearLatestLog()
os.system("cls")
Registry.read(OSRegistryPath)
Registry.set('AOS', 'Quit', "False")
with open(OSRegistryPath, "w") as Registryfile:
Registry.write(Registryfile)
Registry.set('AOS', 'Reboot', "False")
with open(OSRegistryPath, "w") as Registryfile:
Registry.write(Registryfile)
OSVersion = Registry.get('AOS', 'version')
Utils.OSPrint(f"{OSVersion} starting.")
Utils.OSLoad("Booting sequence initializing...", "Booting sequence initialized.", "Slow")
Utils.OSLoad("Booting sequence completing...", "Booting sequence completed.", "Normal")
time.sleep(1)
Utils.OSPrint("Running on kernel ApertureScienceKernel ver 1. Processing speed of 4294967296 bytes/ms.")
winsound.PlaySound(GetAbsolutePath("sounds/startup.wav"), winsound.SND_FILENAME)
OSMain(False)
def CheckIfLoginExists(login):
if not os.path.exists(GetAbsolutePath(f"accounts/{login}.json")):
winsound.PlaySound(GetAbsolutePath("sounds/error2.wav"), winsound.SND_FILENAME)
return False
winsound.PlaySound(GetAbsolutePath("sounds/correct.wav"), winsound.SND_FILENAME)
return True
def SetCurrentUser(login):
Registry.read(OSRegistryPath)
Registry.set('AOS', 'CurrentUser', login)
with open(OSRegistryPath, "w") as Registryfile:
Registry.write(Registryfile)
def SetUserDirectory(login):
Registry.read(OSRegistryPath)
login = Registry.get('AOS', 'CurrentUser')
if Registry.get('AOS', 'UserDirectory') == "":
UserDirectory = f"A/users/{login}/personal_files/"
Registry.read(OSRegistryPath)
Registry.set('AOS', 'UserDirectory', UserDirectory)
with open(OSRegistryPath, "w") as Registryfile:
Registry.write(Registryfile)
def CheckIfPasswordExists(login, password):
with open(GetAbsolutePath(f"accounts/{login}.json")) as f:
data = json.loads(f.read())
if data['password'] != password:
winsound.PlaySound(GetAbsolutePath("sounds/error2.wav"), winsound.SND_FILENAME)
return False
winsound.PlaySound(GetAbsolutePath("sounds/correct.wav"), winsound.SND_FILENAME)
return True
def GetAccreditationLevel(login):
with open(GetAbsolutePath(f"accounts/{login}.json")) as f:
data = json.loads(f.read())
return data["accreditation"]
def CheckIfCriticalFoldersExist(login):
CheckIfADriveExists()
CheckIfLogsFolderExists()
CheckIfUsersFolderExists()
CheckIfCurrentUserFolderExists(login)
CheckIfPersonalFilesFolderExists(login)
def CheckIfADriveExists():
if not os.path.exists(GetAbsolutePath("A")):
try:
os.mkdir(GetAbsolutePath("./A"))
except:
Utils.OSPrintError("FATAL ERROR: Could not create A drive")
sys.exit(1)
def CheckIfLogsFolderExists():
if not os.path.exists(GetAbsolutePath("A/logs/")):
try:
os.mkdir(GetAbsolutePath("A/logs/"))
except:
Utils.OSPrintError("FATAL ERROR: Could not create logs folder")
sys.exit(1)
try:
with open(GetAbsolutePath("A/logs") + ".meta", "wb") as file:
binary_data = struct.pack('i', 2)
file.write(binary_data)
file.close()
except:
Utils.OSPrintError("FATAL ERROR: Could not create logs meta file")
sys.exit(1)
def CheckIfUsersFolderExists():
if not os.path.exists(GetAbsolutePath("A/users/")):
try:
os.mkdir(GetAbsolutePath("A/users/"))
except:
Utils.OSPrintError("FATAL ERROR: Could not create users folder")
sys.exit(1)
def CheckIfCurrentUserFolderExists(login):
if not os.path.exists(GetAbsolutePath(f"A/users/{login}")):
try:
os.mkdir(GetAbsolutePath(f"A/users/{login}"))
except:
Utils.OSPrintError(f"FATAL ERROR: Could not create {login} folder")
sys.exit(1)
def CheckIfPersonalFilesFolderExists(login):
if not os.path.exists(GetAbsolutePath(f"A/users/{login}/personal_files")):
try:
os.mkdir(GetAbsolutePath(f"A/users/{login}/personal_files"))
except:
Utils.OSPrintError("FATAL ERROR: Could not create personal files folder")
sys.exit(1)
def TokenizeCommand(command):
tokens = []
current_token = ''
in_quotes = False
for char in command:
if char == ' ' and not in_quotes:
if current_token:
tokens.append(current_token)
current_token = ''
elif char == '"':
in_quotes = not in_quotes
else:
current_token += char
if current_token:
tokens.append(current_token)
return tokens
def OSMain(LoginFail):
Registry.set('AOS', 'LoggedOut', "False")
with open(OSRegistryPath, "w") as Registryfile:
Registry.write(Registryfile)
if LoginFail:
Utils.OSPrint("Please enter your credentials. Enter login below.")
else:
Utils.OSPrint("Welcome to the Aperture Science OS. Please enter your credentials. Enter login below.")
login = Utils.OSInput(True)
LoginCheck = CheckIfLoginExists(login)
if not LoginCheck:
Utils.OSPrintWarning("Login does not exist! Please try again")
OSMain(True)
Utils.OSPrint("Login successfully identified. Please enter your password below.")
SetCurrentUser(login)
SetUserDirectory(login)
password = Utils.OSInput(True)
PasswordCheck = CheckIfPasswordExists(login, password)
if not PasswordCheck:
Utils.OSPrintWarning("Password is incorrect! Please try again")
OSMain(True)
Utils.OSPrint(f"Password matching. Logging to account \"{login}\"")
time.sleep(1)
AccreditationLevel = GetAccreditationLevel(login)
CheckIfCriticalFoldersExist(login)
Utils.OSPrint(f"Logged in. Account: \"{login}\". Level {AccreditationLevel} Accreditation.")
time.sleep(0.5)
Utils.OSPrint(f"Hello, user \"{login}\". What do you want to do? Enter \"help\" to show commands. Enter \"dir getname\" to show current directory, and its files. Enter \"exec\" to run a program. Enter \"open\" to open a file.")
while True:
Registry.read(OSRegistryPath)
if Registry.get('AOS', 'Reboot') == "True":
LoadOS()
elif Registry.get('AOS', 'loggedout') == "True":
OSMain(False)
elif Registry.get('AOS', 'Quit') == "True":
sys.exit(0)
command = Utils.OSInput(True)
tokens = TokenizeCommand(command)
try:
command_lower = tokens[0].lower()
except:
command_lower = ""
if command_lower not in commands:
Utils.OSPrintWarning("Invalid command. Enter \"help\" to see available commands.")
continue
if len(tokens) > 1:
try:
commands[command_lower](*tokens[1:]) # Pass all arguments
except Exception as e:
if "takes 1 positional argument" in str(e) or "takes from 1 to 2 positional arguments" in str(e):
Utils.OSPrintError(f"ERROR: Command \"{command_lower}\" has too many arguments!")
else:
Utils.OSPrintError(f"UNKNOWN ERROR: {e}")
else:
try:
commands[command_lower]()
except Exception as e:
if "missing 1 required positional argument" in str(e):
Utils.OSPrintError(f"ERROR: Command \"{command_lower}\" has too few arguments!")
else:
Utils.OSPrintError(f"UNKNOWN ERROR: {e}")
LoadOS()