Merge pull request #817 from nonameShijian/PR-Branch

initReadWriteFunction添加是否可以进行文件读写的验证
This commit is contained in:
Spmario233 2024-01-19 23:59:43 +08:00 committed by GitHub
commit 2c5576d7f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 116 additions and 102 deletions

View File

@ -18,118 +18,130 @@
</script>
<script>
if (location.href.startsWith('http') && typeof window.initReadWriteFunction != 'function' && !window.require && !window.__dirname) {
window.initReadWriteFunction = function(game) {
/*game.download = function() {
// 暂不实现
};*/
game.createDir = function (dir, success = () => {}, error = () => {}) {
fetch(`./createDir?dir=${dir}`)
window.initReadWriteFunction = async function(game) {
return new Promise((resolve, reject) => {
fetch(`./readFile?fileName=noname.js`)
.then(response => {
return response.json();
})
.then(result => {
if (result && result.success) success();
else error();
if (result && result.success) callback();
else reject(result.errorMsg)
})
.catch(error);
};
.catch(reject);
game.readFile = function (fileName, callback = () => {}, error = () => {}) {
fetch(`./readFile?fileName=${fileName}`)
.then(response => {
return response.json();
})
.then(result => {
if (result && result.success) callback(new Uint8Array(result.data).buffer);
else error(result && result.errorMsg);
})
.catch(error);
};
game.readFileAsText = function (fileName, callback = () => {}, error = () => {}) {
fetch(`./readFileAsText?fileName=${fileName}`)
.then(response => {
return response.json();
})
.then(result => {
if (result && result.success) callback(result.data);
else error(result && result.errorMsg);
})
.catch(error);
};
game.writeFile = function (data, path, name, callback = () => { }) {
game.ensureDirectory(path, function () {
if (Object.prototype.toString.call(data) == '[object File]') {
const fileReader = new FileReader();
fileReader.onload = function (e) {
game.writeFile(e.target.result, path, name, callback);
};
fileReader.readAsArrayBuffer(data, "UTF-8");
}
else {
let filePath = path;
if (path.endsWith('/')) {
filePath += name;
} else if (path == "") {
filePath += name;
} else {
filePath += '/' + name;
}
fetch(`./writeFile`, {
method: 'post',
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
data: typeof data == 'string' ? data : Array.prototype.slice.call(new Uint8Array(data)),
path: filePath
function callback() {
game.createDir = function (dir, success = () => { }, error = () => { }) {
fetch(`./createDir?dir=${dir}`)
.then(response => {
return response.json();
})
})
.then(response => {
return response.json();
})
.then(result => {
if (result && result.success) {
callback();
} else {
callback(result.errorMsg);
.then(result => {
if (result && result.success) success();
else error();
})
.catch(error);
};
game.readFile = function (fileName, callback = () => { }, error = () => { }) {
fetch(`./readFile?fileName=${fileName}`)
.then(response => {
return response.json();
})
.then(result => {
if (result && result.success) callback(new Uint8Array(result.data).buffer);
else error(result && result.errorMsg);
})
.catch(error);
};
game.readFileAsText = function (fileName, callback = () => { }, error = () => { }) {
fetch(`./readFileAsText?fileName=${fileName}`)
.then(response => {
return response.json();
})
.then(result => {
if (result && result.success) callback(result.data);
else error(result && result.errorMsg);
})
.catch(error);
};
game.writeFile = function (data, path, name, callback = () => { }) {
game.ensureDirectory(path, function () {
if (Object.prototype.toString.call(data) == '[object File]') {
const fileReader = new FileReader();
fileReader.onload = function (e) {
game.writeFile(e.target.result, path, name, callback);
};
fileReader.readAsArrayBuffer(data, "UTF-8");
}
else {
let filePath = path;
if (path.endsWith('/')) {
filePath += name;
} else if (path == "") {
filePath += name;
} else {
filePath += '/' + name;
}
fetch(`./writeFile`, {
method: 'post',
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
data: typeof data == 'string' ? data : Array.prototype.slice.call(new Uint8Array(data)),
path: filePath
})
})
.then(response => {
return response.json();
})
.then(result => {
if (result && result.success) {
callback();
} else {
callback(result.errorMsg);
}
});
}
});
}
});
};
};
game.removeFile = function (fileName, callback = () => {}) {
fetch(`./removeFile?fileName=${fileName}`)
.then(response => {
return response.json();
})
.then(result => {
callback(result.errorMsg);
})
.catch(error);
};
game.removeFile = function (fileName, callback = () => { }) {
fetch(`./removeFile?fileName=${fileName}`)
.then(response => {
return response.json();
})
.then(result => {
callback(result.errorMsg);
})
.catch(error);
};
game.getFileList = function (dir, callback = () => {}) {
fetch(`./getFileList?dir=${dir}`)
.then(response => {
return response.json();
})
.then(result => {
if (result && result.success) {
callback(result.data.dirs, result.data.files);
game.getFileList = function (dir, callback = () => { }) {
fetch(`./getFileList?dir=${dir}`)
.then(response => {
return response.json();
})
.then(result => {
if (result && result.success) {
callback(result.data.dirs, result.data.files);
}
});
};
game.ensureDirectory = function (list, callback = () => { }, file = false) {
let pathArray = typeof list == "string" ? list.split("/") : list;
if (file) {
pathArray = pathArray.slice(0, -1);
}
});
};
game.createDir(pathArray.join("/"), callback, console.error);
};
game.ensureDirectory = function (list, callback = () => {}, file = false) {
let pathArray = typeof list == "string" ? list.split("/") : list;
if (file) {
pathArray = pathArray.slice(0, -1);
resolve();
}
game.createDir(pathArray.join("/"), callback, console.error);
};
});
};
}
</script>

View File

@ -68,8 +68,8 @@ declare interface Window {
get: Get;
ai: AI;
}
initReadWriteFunction?(game: Game): void;
/** 为其他自定义平台提供文件读写函数赋值的一种方式 */
initReadWriteFunction?(game: Game): Promise<void>;
bannedKeyWords: string[];
}

View File

@ -106,7 +106,7 @@ export async function boot() {
else if (!Reflect.has(lib, 'device')) {
Reflect.set(lib, 'path', (await import('../library/path.js')).default);
//为其他自定义平台提供文件读写函数赋值的一种方式。
//但这种方式只修改game的文件读写函数。
//但这种方式只允许修改game的文件读写函数。
if (typeof window.initReadWriteFunction == 'function') {
const g = {};
const ReadWriteFunctionName = ['download', 'readFile', 'readFileAsText', 'writeFile', 'removeFile', 'getFileList', 'ensureDirectory', 'createDir'];
@ -123,7 +123,9 @@ export async function boot() {
});
});
// @ts-ignore
window.initReadWriteFunction(g);
await window.initReadWriteFunction(g).catch(e => {
console.error('文件读写函数初始化失败:', e);
});
}
window.onbeforeunload = function () {
if (config.get('confirm_exit') && !_status.reloading) {