First functional userscript loading/unloading

This commit is contained in:
ebolam
2022-08-30 11:12:49 -04:00
parent d6c6db64c4
commit e54bc2564d
3 changed files with 119 additions and 5 deletions

View File

@@ -6837,7 +6837,7 @@ def popup_change_file(data):
def file_popup(popup_title, starting_folder, return_event, upload=True, jailed=True, folder_only=True, renameable=False, deleteable=False,
editable=False, show_breadcrumbs=True, item_check=None, show_hidden=False,
valid_only=False, hide_extention=False, extra_parameter_function=None,
column_names=['File Name'], show_filename=True,
column_names=['File Name'], show_filename=True, show_folders=True,
column_widths=["100%"],
sort="Modified", desc=False):
#starting_folder = The folder we're going to get folders and/or items from
@@ -6871,6 +6871,7 @@ def file_popup(popup_title, starting_folder, return_event, upload=True, jailed=T
session['column_widths'] = column_widths
session['sort'] = sort
session['desc'] = desc
session['show_folders'] = show_folders
socketio.emit("load_popup", {"popup_title": popup_title, "call_back": return_event, "renameable": renameable, "deleteable": deleteable, "editable": editable, 'upload': upload}, broadcast=False, room="UI_2")
socketio.emit("load_popup", {"popup_title": popup_title, "call_back": return_event, "renameable": renameable, "deleteable": deleteable, "editable": editable, 'upload': upload}, broadcast=True, room="UI_1")
@@ -6892,6 +6893,7 @@ def get_files_folders(starting_folder):
column_widths = session['column_widths']
sort = session['sort']
desc = session['desc']
show_folders = session['show_folders']
if starting_folder == 'This PC':
breadcrumbs = [['This PC', 'This PC']]
@@ -6945,7 +6947,10 @@ def get_files_folders(starting_folder):
else:
files.append([False, item_full_path, item, valid_selection, extra_parameters])
if show_folders:
items = folders
else:
items = []
if not folder_only:
items += files
@@ -7413,7 +7418,7 @@ def handle_exception(e):
#==================================================================#
# Event triggered when Softprompt is clicked
# Event triggered when Softprompt load menu is clicked
#==================================================================#
@socketio.on('load_softprompt_list')
def UI_2_load_softprompt_list(data):
@@ -7445,15 +7450,90 @@ def get_softprompt_desc(item_full_path, item, valid_selection):
ob = json.load(f)
return [ob['name'], ob['description']]
#==================================================================#
# Event triggered when Softprompt is clicked
# Event triggered when Softprompt is loaded
#==================================================================#
@socketio.on('load_softprompt')
def UI_2_load_softprompt(data):
print("Load softprompt: {}".format(data))
spRequest(data)
#==================================================================#
# Event triggered when load userscripts is clicked
#==================================================================#
@socketio.on('load_userscripts_list')
def UI_2_load_userscripts_list(data):
file_popup("Select Userscripts to Load", "./userscripts", "load_userscripts", upload=True, jailed=True, folder_only=False, renameable=True, editable=True,
deleteable=True, show_breadcrumbs=False, item_check=valid_userscripts_to_load,
valid_only=True, hide_extention=True, extra_parameter_function=get_userscripts_desc,
column_names=['Module Name', 'Description'],
show_filename=True, show_folders=False,
column_widths=['200px', '150px', 'auto'])
def valid_userscripts_to_load(file):
return file.endswith(".lua") and file not in koboldai_vars.userscripts
def valid_userscripts_to_unload(file):
return file.endswith(".lua") and file in koboldai_vars.userscripts
def get_userscripts_desc(item_full_path, item, valid_selection):
if not valid_selection:
return [None, None]
ob = ["", ""]
description = []
multiline = False
with open(item_full_path) as f:
ob[0] = f.readline().strip().replace("\033", "")
if ob[0][:2] != "--":
ob[0] = file
else:
ob[0] = ob[0][2:]
if ob[0][:2] == "[[":
ob[0] = ob[0][2:]
multiline = True
ob[0] = ob[0].lstrip("-").strip()
for line in f:
line = line.strip().replace("\033", "")
if multiline:
index = line.find("]]")
if index > -1:
description.append(line[:index])
if index != len(line) - 2:
break
multiline = False
else:
description.append(line)
else:
if line[:2] != "--":
break
line = line[2:]
if line[:2] == "[[":
multiline = True
line = line[2:]
description.append(line.strip())
ob[1] = "\n".join(description)
if len(ob[1]) > 250:
ob[1] = ob[1][:247] + "..."
return ob
#==================================================================#
# Event triggered when userscript's are loaded
#==================================================================#
@socketio.on('load_userscripts')
def UI_2_load_userscripts(data):
print("Loading Userscripts: {}".format(os.path.basename(data)))
koboldai_vars.userscripts = [x for x in koboldai_vars.userscripts if x != os.path.basename(data)]+[os.path.basename(data)]
load_lua_scripts()
#==================================================================#
# Event triggered when userscript's are unloaded
#==================================================================#
@socketio.on('unload_userscripts')
def UI_2_unload_userscripts(data):
print("Unloading Userscript: {}".format(data))
koboldai_vars.userscripts = [x for x in koboldai_vars.userscripts if x != data]
load_lua_scripts()
#==================================================================#

View File

@@ -661,6 +661,13 @@ function popup_items(data) {
//add dynamic columns
var i = 0;
if (data.show_filename) {
td = document.createElement("span");
td.textContent = "File Name";
td.style = "grid-area: p"+i+";";
i+=1;
tr.append(td)
}
for (column of data.column_names) {
td = document.createElement("span");
td.textContent = column;
@@ -1692,6 +1699,15 @@ function do_wpp(wpp_area) {
}
//--------------------------------------------UI to Server Functions----------------------------------
function unload_userscripts() {
files_to_unload = document.getElementById('loaded_userscripts');
for (var i=0; i<files_to_unload.options.length; i++) {
if (files_to_unload.options[i].selected) {
socket.emit("unload_userscripts", files_to_unload.options[i].value);
}
}
}
function save_theme() {
var cssVars = getAllCSSVariableNames();
for (const [key, value] of Object.entries(cssVars)) {

View File

@@ -177,6 +177,24 @@
<span class="setting_minlabel"><span style="top: -4px; position: relative;"></span></span>
<span class="setting_maxlabel"><span style="top: -4px; position: relative;"></span></span>
</div>
<div class="setting_container_single">
<!---Top Row---->
<span class="setting_label">
<span style="white-space: pre-wrap;">User Scripts: </span>
<span class="helpicon material-icons-outlined" title="Allows for user scripts that modify the way the game functions">help_icon</span>
</span>
<!---Bottom Row---->
<span class="setting_item">
<div style="display: flex;flex-direction: row;">
<select id="loaded_userscripts" class="var_sync_system_userscripts settings_select" multiple></select>
<span class="material-icons-outlined cursor" style="font-size: 18px;" title="Unload File(s)" onclick="unload_userscripts();">delete</span>
<span class="material-icons-outlined cursor" style="font-size: 18px;" title="Load File" onclick="socket.emit('load_userscripts_list', '');">upload_file</span>
</div>
</span>
<!---Slider Labels--->
<span class="setting_minlabel"><span style="top: -4px; position: relative;"></span></span>
<span class="setting_maxlabel"><span style="top: -4px; position: relative;"></span></span>
</div>
</div>
{% endwith %}
<div class="collapsable_header" onclick="toggle_setting_category(this);">