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>
<script> <script>
if (location.href.startsWith('http') && typeof window.initReadWriteFunction != 'function' && !window.require && !window.__dirname) { if (location.href.startsWith('http') && typeof window.initReadWriteFunction != 'function' && !window.require && !window.__dirname) {
window.initReadWriteFunction = function(game) { window.initReadWriteFunction = async function(game) {
/*game.download = function() { return new Promise((resolve, reject) => {
// 暂不实现 fetch(`./readFile?fileName=noname.js`)
};*/
game.createDir = function (dir, success = () => {}, error = () => {}) {
fetch(`./createDir?dir=${dir}`)
.then(response => { .then(response => {
return response.json(); return response.json();
}) })
.then(result => { .then(result => {
if (result && result.success) success(); if (result && result.success) callback();
else error(); else reject(result.errorMsg)
}) })
.catch(error); .catch(reject);
};
game.readFile = function (fileName, callback = () => {}, error = () => {}) { function callback() {
fetch(`./readFile?fileName=${fileName}`) game.createDir = function (dir, success = () => { }, error = () => { }) {
.then(response => { fetch(`./createDir?dir=${dir}`)
return response.json(); .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(result => {
.then(response => { if (result && result.success) success();
return response.json(); else error();
}) })
.then(result => { .catch(error);
if (result && result.success) { };
callback();
} else { game.readFile = function (fileName, callback = () => { }, error = () => { }) {
callback(result.errorMsg); 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 = () => {}) { game.removeFile = function (fileName, callback = () => { }) {
fetch(`./removeFile?fileName=${fileName}`) fetch(`./removeFile?fileName=${fileName}`)
.then(response => { .then(response => {
return response.json(); return response.json();
}) })
.then(result => { .then(result => {
callback(result.errorMsg); callback(result.errorMsg);
}) })
.catch(error); .catch(error);
}; };
game.getFileList = function (dir, callback = () => {}) { game.getFileList = function (dir, callback = () => { }) {
fetch(`./getFileList?dir=${dir}`) fetch(`./getFileList?dir=${dir}`)
.then(response => { .then(response => {
return response.json(); return response.json();
}) })
.then(result => { .then(result => {
if (result && result.success) { if (result && result.success) {
callback(result.data.dirs, result.data.files); 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) { resolve();
let pathArray = typeof list == "string" ? list.split("/") : list;
if (file) {
pathArray = pathArray.slice(0, -1);
} }
game.createDir(pathArray.join("/"), callback, console.error); });
};
}; };
} }
</script> </script>

View File

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

View File

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