-
Notifications
You must be signed in to change notification settings - Fork 14
/
ttle.py
263 lines (220 loc) · 10.7 KB
/
ttle.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
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
""" OpenLevelEditor Base Class - Drewcification 091420 """
import argparse
import asyncio
import builtins
import os
import pathlib
import sys
from direct.directnotify.DirectNotifyGlobal import directNotify
from direct.gui.OnscreenText import OnscreenText
from direct.showbase.ShowBase import ShowBase
from panda3d.core import loadPrcFile, loadPrcFileData
from tkinter import Tk, messagebox
from ott.Settings import Settings
from ott.ShaderRegistry import ShaderRegistry
from toontown.toonbase import ToontownGlobals
TOONTOWN_ONLINE = 0
TOONTOWN_REWRITTEN = 1
TOONTOWN_CORPORATE_CLASH = 2
TOONTOWN_OFFLINE = 3
SERVER_TO_ID = {'online': TOONTOWN_ONLINE,
'rewritten': TOONTOWN_REWRITTEN,
'clash': TOONTOWN_CORPORATE_CLASH,
'offline': TOONTOWN_OFFLINE
}
DEFAULT_SERVER = TOONTOWN_ONLINE
DEFAULT_SETTINGS = {
'autosave-enabled': True,
'autosave-interval': 15,
'autosave-max-files': 10,
'fps-meter-update-rate': 0,
'panel-scaling': 1.0
}
class ToontownLevelEditor(ShowBase):
notify = directNotify.newCategory("Open Level Editor")
APP_VERSION = open('ver', 'r').read()
def __init__(self):
# Load the prc file prior to launching showbase in order
# to have it affect window related stuff
loadPrcFile('editor.prc')
builtins.userfiles = self.config.GetString('userfiles-directory')
if not os.path.exists(userfiles):
pathlib.Path(userfiles).mkdir(parents = True, exist_ok = True)
builtins.settings = Settings(f'{userfiles}/settings.json')
for setting in DEFAULT_SETTINGS:
if setting not in settings:
settings[setting] = DEFAULT_SETTINGS[setting]
# Check for -e or -d launch options
parser = argparse.ArgumentParser(description = "Modes")
parser.add_argument("--experimental", action = 'store_true', help = "Enables experimental features")
parser.add_argument("--debug", action = 'store_true', help = "Enables debugging features")
parser.add_argument("--noupdate", action = 'store_true', help = "Disables Auto Updating")
parser.add_argument("--png", action = 'store_true', help = "Forces PNG resources mode, if this is not "
"specified, it will automatically determine the "
"format")
parser.add_argument("--compiler", nargs = "*",
help = "Specify which compiler to use (Only useful if your game uses a form of "
"libpandadna.) Valid options are 'libpandadna', for games which use the "
"modern c++ version of libpandadna, and 'clash', for Corporate Clash")
parser.add_argument("--server", nargs = "*", help = "Enables features exclusive to various Toontown projects",
default = 'online')
parser.add_argument("--holiday", nargs = "*", help = "Enables holiday modes. [halloween or winter]")
parser.add_argument("--hoods", nargs = "*", help = "Only loads the storage files of the specified hoods",
default = ['TT', 'DD', 'BR', 'DG',
'DL', 'MM', 'GS', 'GZ',
'SBHQ', 'LBHQ', 'CBHQ', 'BBHQ',
'OZ', 'PA', 'ES', 'TUT'])
parser.add_argument("--minigame", nargs="*", help = "Enables features specific to certain minigames")
parser.add_argument("dnaPath", nargs = "?", help = "Load the DNA file through the specified path")
args = parser.parse_args()
if args.experimental:
loadPrcFileData("", "want-experimental true")
if args.debug:
loadPrcFileData("", "want-debug true")
if args.compiler:
loadPrcFileData("", f"compiler {args.compiler[0]}")
if args.holiday:
loadPrcFileData("", f"holiday {args.holiday[0]}")
if args.png:
loadPrcFileData("", "png-textures true")
if args.minigame:
loadPrcFileData("", f"minigame {args.minigame[0]}")
else:
# If we don't specify png, we can search
# we can use the eyes texture
if os.path.exists("phase_3/maps/eyes.jpg"):
loadPrcFileData("", "png-textures false")
elif os.path.exists("phase_3/maps/eyes.png"):
loadPrcFileData("", "png-textures true")
else:
messagebox.showerror(
message = "There was an error located resources!\n"
"Make sure you put the phase folders in the root folder!")
server = SERVER_TO_ID.get(args.server[0].lower(), DEFAULT_SERVER)
self.server = server
self.hoods = args.hoods
# HACK: Check for dnaPath in args.hoods
for hood in self.hoods[:]:
if hood.endswith('.dna'):
args.dnaPath = hood
args.hoods.remove(hood)
break
# Check for any files we need and such
self.__checkForFiles()
# Import the main dlls so we don't have to repeatedly import them everywhere
self.__importMainLibs()
# Setup the root for Tkinter!
self.__createTk()
if not args.noupdate:
loop = asyncio.get_event_loop()
loop.run_until_complete(self.__checkUpdates())
self.__addCullBins()
self.__registerShaders()
# Now we actually start the editor
ShowBase.__init__(self)
aspect2d.setAntialias(AntialiasAttrib.MAuto)
# Create the framerate meter
flag = self.config.GetBool('show-frame-rate-meter', False)
if flag:
self.toggleFrameRateMeter(flag)
from toontown.leveleditor import LevelEditor
self.le = LevelEditor.LevelEditor()
self.le.startUp(args.dnaPath)
def setFrameRateMeter(self, flag):
return
def toggleFrameRateMeter(self, flag):
if flag:
if not self.frameRateMeter:
self.frameRateMeter = OnscreenText(parent = base.a2dTopRight, text = '', pos = (-0.01, -0.05, 0.0),
scale = 0.05, style = 3, bg = (0, 0, 0, 0.4),
align = TextNode.ARight,
font = ToontownGlobals.getToonFont())
taskMgr.add(self.updateFrameRateMeter, 'fps')
else:
if self.frameRateMeter:
self.frameRateMeter.destroy()
self.frameRateMeter = None
def updateFrameRateMeter(self, task):
"""
Base code inspired from
https://discourse.panda3d.org/t/trying-to-create-custom-fps-counter/25328/15
"""
fps = globalClock.getAverageFrameRate()
if fps <= 45:
# At or below 45 fps is yellow
color = (1, 0.9, 0, 1)
elif fps <= 30:
# At or below 30 fps is red
color = (1, 0, 0, 1)
else:
# Color is green by default
color = (0, 0.9, 0, 1)
text = f'{round(fps, 1)} FPS'
self.frameRateMeter.setText(text)
self.frameRateMeter.setFg(color)
task.delayTime = settings['fps-meter-update-rate'] / 1000
return task.again
def __checkForFiles(self):
# Make custom hood directory if it doesn't exist
if not os.path.exists(f'{userfiles}/hoods/'):
os.mkdir(f'{userfiles}/hoods/')
# Make a maps directory if we don't have one
if not os.path.isdir("maps"):
os.mkdir("maps")
# Make a Screenshots directory if we don't have one
if not os.path.isdir("screenshots"):
os.mkdir("screenshots")
def __importMainLibs(self):
builtin_dict = builtins.__dict__
builtin_dict.update(__import__('panda3d.core', fromlist = ['*']).__dict__)
try:
# detect if using a panda with libtoontown baked in
builtin_dict.update(__import__('panda3d.toontown', fromlist = ['*']).__dict__)
except:
# using open-source version
builtin_dict.update(__import__('libotp', fromlist = ['*']).__dict__)
builtin_dict.update(__import__('libtoontown', fromlist = ['*']).__dict__)
def __createTk(self):
tkroot = Tk()
tkroot.withdraw()
tkroot.tk.call('tk', 'scaling', settings.get('panel-scaling', 1.0))
tkroot.title("Open Level Editor")
if sys.platform == 'win32':
# FIXME: This doesn't work in other platforms for some reason...
tkroot.iconbitmap("resources/openttle_ico_temp.ico")
self.tkRoot = tkroot
@staticmethod
def __addCullBins():
cbm = CullBinManager.getGlobalPtr()
cbm.addBin('ground', CullBinManager.BTUnsorted, 18)
cbm.addBin('shadow', CullBinManager.BTBackToFront, 19)
@staticmethod
def __registerShaders():
ShaderRegistry.register('render:black_and_white',
frag = 'resources/shaders/tt_sha_render_bandw.frag',
vert = 'resources/shaders/tt_sha_render_bandw.vert')
ShaderRegistry.register('dna:anim_prop',
frag = 'resources/shaders/tt_sha_dna_anim_prop.frag',
vert = 'resources/shaders/tt_sha_dna_anim_prop.vert')
async def __checkUpdates(self):
import aiohttp, webbrowser
async with aiohttp.ClientSession() as session:
try:
async with session.get(
"https://raw.githubusercontent.com/OpenToontownTools/OpenLevelEditor/master/ver") as resp:
ver = await resp.text()
ver = ver.splitlines()[0]
if ver != self.APP_VERSION:
self.notify.info(f"Client is out of date! Latest: {ver} | Client: {self.APP_VERSION}")
if messagebox.askokcancel("Error",
f"Client is out of date!\nLatest: {ver} | Client: {self.APP_VERSION}. "
f"Press OK to be taken to the download page."):
webbrowser.open("https://github.com/OpenToontownTools/OpenLevelEditor/releases/latest")
else:
self.notify.info("Client is up to date!")
except:
messagebox.showerror(
message = "There was an error checking for updates! This is likely an issue with your connection. "
"Press OK to continue using the application.")
# Run it
ToontownLevelEditor().run()