2021-05-07 20:32:10 +02:00
|
|
|
import tkinter as tk
|
|
|
|
from tkinter import filedialog
|
2021-05-22 11:28:40 +02:00
|
|
|
from os import getcwd, listdir, path
|
2021-09-01 00:22:30 +02:00
|
|
|
import os
|
2021-05-22 11:28:40 +02:00
|
|
|
import json
|
2021-05-07 20:32:10 +02:00
|
|
|
|
|
|
|
#==================================================================#
|
|
|
|
# Generic Method for prompting for file path
|
|
|
|
#==================================================================#
|
|
|
|
def getsavepath(dir, title, types):
|
|
|
|
root = tk.Tk()
|
|
|
|
root.attributes("-topmost", True)
|
|
|
|
path = tk.filedialog.asksaveasfile(
|
|
|
|
initialdir=dir,
|
|
|
|
title=title,
|
2021-05-16 01:29:41 +02:00
|
|
|
filetypes = types,
|
|
|
|
defaultextension="*.*"
|
2021-05-07 20:32:10 +02:00
|
|
|
)
|
|
|
|
root.destroy()
|
|
|
|
if(path != "" and path != None):
|
|
|
|
return path.name
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
#==================================================================#
|
|
|
|
# Generic Method for prompting for file path
|
|
|
|
#==================================================================#
|
|
|
|
def getloadpath(dir, title, types):
|
|
|
|
root = tk.Tk()
|
|
|
|
root.attributes("-topmost", True)
|
|
|
|
path = tk.filedialog.askopenfilename(
|
|
|
|
initialdir=dir,
|
|
|
|
title=title,
|
|
|
|
filetypes = types
|
|
|
|
)
|
|
|
|
root.destroy()
|
|
|
|
if(path != "" and path != None):
|
|
|
|
return path
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
#==================================================================#
|
|
|
|
# Generic Method for prompting for directory path
|
|
|
|
#==================================================================#
|
|
|
|
def getdirpath(dir, title):
|
|
|
|
root = tk.Tk()
|
|
|
|
root.attributes("-topmost", True)
|
|
|
|
path = filedialog.askdirectory(
|
|
|
|
initialdir=dir,
|
|
|
|
title=title
|
|
|
|
)
|
|
|
|
root.destroy()
|
|
|
|
if(path != "" and path != None):
|
|
|
|
return path
|
|
|
|
else:
|
2021-05-22 11:28:40 +02:00
|
|
|
return None
|
|
|
|
|
2021-09-01 00:22:30 +02:00
|
|
|
#==================================================================#
|
|
|
|
# Returns the path (as a string) to the given story by its name
|
|
|
|
#==================================================================#
|
|
|
|
def storypath(name):
|
|
|
|
return path.join(path.dirname(path.realpath(__file__)), "stories", name + ".json")
|
|
|
|
|
2021-05-22 11:28:40 +02:00
|
|
|
#==================================================================#
|
|
|
|
# Returns an array of dicts containing story files in /stories
|
|
|
|
#==================================================================#
|
|
|
|
def getstoryfiles():
|
|
|
|
list = []
|
2021-06-26 00:02:19 +02:00
|
|
|
for file in listdir(path.dirname(path.realpath(__file__))+"/stories"):
|
2021-05-22 11:28:40 +02:00
|
|
|
if file.endswith(".json"):
|
|
|
|
ob = {}
|
|
|
|
ob["name"] = file.replace(".json", "")
|
2021-06-26 00:02:19 +02:00
|
|
|
f = open(path.dirname(path.realpath(__file__))+"/stories/"+file, "r")
|
|
|
|
try:
|
|
|
|
js = json.load(f)
|
|
|
|
except:
|
2021-08-25 01:29:40 +02:00
|
|
|
print(f"Browser loading error: {file} is malformed or not a JSON file.")
|
2021-06-26 00:02:19 +02:00
|
|
|
f.close()
|
|
|
|
continue
|
2021-05-22 11:28:40 +02:00
|
|
|
f.close()
|
2021-06-26 00:02:19 +02:00
|
|
|
try:
|
|
|
|
ob["actions"] = len(js["actions"])
|
|
|
|
except TypeError:
|
2021-08-25 01:29:40 +02:00
|
|
|
print(f"Browser loading error: {file} has incorrect format.")
|
2021-06-26 00:17:07 +02:00
|
|
|
continue
|
2021-05-22 11:28:40 +02:00
|
|
|
list.append(ob)
|
|
|
|
return list
|
|
|
|
|
|
|
|
#==================================================================#
|
|
|
|
# Returns True if json file exists with requested save name
|
|
|
|
#==================================================================#
|
|
|
|
def saveexists(name):
|
2021-09-01 00:22:30 +02:00
|
|
|
return path.exists(storypath(name))
|
|
|
|
|
|
|
|
#==================================================================#
|
|
|
|
# Delete save file by name; returns None if successful, or the exception if not
|
|
|
|
#==================================================================#
|
|
|
|
def deletesave(name):
|
|
|
|
try:
|
|
|
|
os.remove(storypath(name))
|
|
|
|
except Exception as e:
|
|
|
|
return e
|
|
|
|
|
|
|
|
#==================================================================#
|
|
|
|
# Rename save file; returns None if successful, or the exception if not
|
|
|
|
#==================================================================#
|
|
|
|
def renamesave(name, new_name):
|
|
|
|
try:
|
|
|
|
os.replace(storypath(name), storypath(new_name))
|
|
|
|
except Exception as e:
|
|
|
|
return e
|