/// <reference types="../../CTAutocomplete" />
/// <reference lib="es2015" />
const Instant = Java.type("java.time.Instant");
import logger from "../logger";
const File = Java.type("java.io.File")
import metadata from "../metadata.js"
import soopyV2Server from "../socketConnection";
import { fetch } from "../utils/networkUtils";
import NonPooledThread from "../utils/nonPooledThread";
import { setRendering } from "../utils/renderJavaUtils";
import { registerForge as registerForgeBase, unregisterForge as unregisterForgeBase } from "./forgeEvents.js"
const JSLoader = Java.type("com.chattriggers.ctjs.engine.langs.js.JSLoader")
const UrlModuleSourceProvider = Java.type("org.mozilla.javascript.commonjs.module.provider.UrlModuleSourceProvider")
const UrlModuleSourceProviderInstance = new UrlModuleSourceProvider(null, null)
const StrongCachingModuleScriptProviderClass = Java.type("org.mozilla.javascript.commonjs.module.provider.StrongCachingModuleScriptProvider")
let StrongCachingModuleScriptProvider = new StrongCachingModuleScriptProviderClass(UrlModuleSourceProviderInstance)
let CTRequire = new JSLoader.CTRequire(StrongCachingModuleScriptProvider)
const System = Java.type("java.lang.System")
let loadedModules = new Set()
let shouldRequireForceNoCache = true
function RequireNoCache(place) {
if (!shouldRequireForceNoCache) {
if (!logger.isDev) return require(place)
if (!loadedModules.has(place)) {
loadedModules.add(place)
return require(place) //performance optimisation
}
}
StrongCachingModuleScriptProvider = new StrongCachingModuleScriptProviderClass(UrlModuleSourceProviderInstance)
CTRequire = new JSLoader.CTRequire(StrongCachingModuleScriptProvider)
return CTRequire(place)
}
class FeatureManager {
constructor() {
this.isDev = logger.isDev
this.messagePrefix = "&6[SOOPY V2]&7 "
this.enabled = true //make triggers work with this context
this.features = {};
this.events = {}
this.eventObjects = {}
this.soopyEventHandlers = {}
this.parent = undefined
this.finishedLoading = false
this.commandFuncs = {}
this.lastEventId = 0
this.customEvents = {}
this.lastChatEventId = 0
this.forgeEvents = {}
this.lastForgeEventId = 0
this.lastSoopyEventId = 0
this.featureSettingsDataLastUpdated = false
//PERFORMANCE RECORDING
this.recordingPerformanceUsage = false
this.performanceUsage = {} //{<moduleName>: {<event>: {time: 0, count: 0}}}
this.flameGraphData = []
this.longEventTime = 20
this.perfTrackingFeatures = false
this.stack = []
this.featureMetas = {}
this.featureSettingsData = {}
let fetchD = fetch("http://soopy.dev/api/soopyv2/ping").async(d => {
if (fetchD.responseCode() >= 400 || fetchD.responseCode() === -1) {
ChatLib.chat(this.messagePrefix + "&cError: Could not connect to Soopy's server. This may cause issues with some features but will (hopefully) be back soon.")
}
new Thread(() => {
this.loadSoopy()
}).start()
}, true)
this.registerStep(false, 30, () => {
if (this.featureSettingsDataLastUpdated) {
new Thread(() => {
this.saveFeatureSettings()
}).start()
}
}, this)
this.registerEvent("worldUnload", this.saveFeatureSettings, this)
this.registerEvent("gameUnload", () => {
this.saveFeatureSettings()
this.unloadAllFeatures()
this.enabled = false
}, this)
if (this.isDev) {
this.registerStep(true, 2, () => {
if (this.reloadModuleTime !== 0 && Date.now() - this.reloadModuleTime > 0) {
new Thread(() => {
this.reloadModuleTime = 0
this.reloadingModules.forEach(m => {
this.unloadFeature(m)
})
this.reloadingModules.forEach(m => {
this.loadFeature(m)
})
this.reloadingModules.clear()
}).start()
}
}, this)
this.watches = {}
this.addedWatches = new Set()
this.watchService = Java.type("java.nio.file.FileSystems").getDefault().newWatchService();
this.reloadingModules = new Set()
this.reloadModuleTime = 0
new NonPooledThread(() => {
while (this.enabled) {
key = this.watchService.take();
let moduleToReload = this.watches[key]
if (this.features[moduleToReload] && !this.reloadingModules.has(moduleToReload)) { //if enabled && not alr in queue
this.reloadingModules.add(moduleToReload)
this.reloadModuleTime