设为首页收藏本站同能贴吧 切换语言 繁体中文
开启辅助访问 切换到窄版
扫描二维码关注官方公众号
返回列表
+ 发新帖
查看: 64|回复: 0

[转载发布] Load notes or parts of notes from text files - IncludeNotesFromFile.js

[复制链接]
累计送礼:
0 个
累计收礼:
1 个
  • TA的每日心情
    开心
    2026-7-12 04:10
  • 签到天数: 209 天

    连续签到: 2 天

    [LV.7]常住居民III

    7977

    主题

    864

    回帖

    3万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    VIP
    7
    卡币
    29961
    OK点
    16
    推广点
    0
    同能卷
    50
    积分
    38832

    灌水之王

    发表于 4 天前 | 显示全部楼层 |阅读模式
    IncludeNotesFromFile.js v1.0.0 - by _neverGiveUp

    Introduction


    This plugin allows you to load notes or parts of notes from text files.
    With IncludeNotesFromFile.js, any tag of the form <include: ____> in the notes field of any game object will be swapped out with the contents of the file data/notes/____.txt. So <include: a> would load a.txt from folder data/notes, <include: b/c> would load c.txt from folder data/notes/b, etc.

    The intention here is to effectively allow the construction of load-time mixins with respect to note tag code for other plugins, so as to reduce code duplication across database entries. For instance, if you used this plugin with an enemy traits plugin, you could create prefab'd collections of enemy traits, and mix and match them with just a single note tag (<include: ...>) per collection, rather than copying and pasting them all over the place.

    Features

    This plugin is an overhaul of Victor Sant's VE_NotesTextFile.js (https://victorenginescripts.wordpress.com/rpg-maker-mv/notes-text-file/ ). Huge thanks to Victor Sant, of course, for writing the original plugin and giving me something to study and adapt to my own needs.

    The distinguishing features of this plugin versus VE_NotesTextFile.js are that IncludeNotesFromFile.js is able to cache loaded text files and perform recursive parsing and loading. By "cache loaded text files" I mean that it will only have to fetch any given data/notes/*.txt a single time during loading, even if it's <include:>'d multiple times, and by "recursive parsing and loading" I mean that files in data/notes can reference other files, e.g. if a note directly contains the tag <include: a> and a.txt contains the tag <include: b>, then b.txt will be loaded, rather than the raw text <include: b> being substituted into the note.

    How to Use

    The folder data/notes will have to be created and manually populated with any .txt files you want to be able to load into note fields. Other than that, I believe I've adequately described the usage of this plugin under Introduction. Its feature set is really very small.

    One thing I should mention is that this plugin should be placed at or near the top of the list of plugins -- at least before any plugins you want it to be compatible with. The reason is that many plugins parse note tags by overriding database load behavior, and this plugin does as well, but the overrides it provides modify the note tags, and other plugins need to be able to see those modifications if you want them to see and use the tags contained in your data/notes/*.txt files.

    Script

    Spoiler                JavaScript:       
    1. /*:
    2. * @plugindesc Note tag text file loader supporting recursive loading.
    3. * Credit to Victor Sant for his plugin VE_NotesTextFile.js,
    4. * whose code I studied for this.
    5. * @author _neverGiveUp
    6. * @help
    7. * To ensure that other plugins' DataManager overrides
    8. * do not procede those defined here,
    9. * and thus that the note data that other plugins see
    10. * is that produced by parsing the includes,
    11. * you should place this plugin at the top of the list.
    12. *
    13. * To include a text file in a note tag, use <include: FILENAME>,
    14. * where FILENAME is a text file in data/notes,
    15. * without the preceding data/notes or the following .txt.
    16. *
    17. * For example: <include: myNote> would include data/notes/myNote.txt.
    18. *
    19. * Subdirectories are also supported.
    20. *
    21. * For example: <include: myDir/myNote>
    22. * would include data/notes/myDir/myNote.txt.
    23. *
    24. * The distinguishing feature of this plugin versus Victor's implementation
    25. * is that this plugin supports recursively processing include tags
    26. * that were loaded in by processing other include tags,
    27. * thus allowing one note prefab file to include another.
    28. *
    29. * You should not abuse this functionality to create recursive include loops
    30. * or else the game will crash on load.
    31. */
    32. (() => {
    33.    
    34.     let INFF = {};
    35.     INFF.Cache = {};
    36.     INFF.Jobs = [];
    37.     INFF.Regex = new RegExp('<include:\\s*(.+)>');
    38.     INFF.Status = {};
    39.     INFF.Status.Parsing = 0;
    40.     INFF.Status.Loading = 1;
    41.     INFF.Status.Done = 2;
    42.     INFF.DEBUG = false;
    43.     INFF.debug = function () {
    44.         if (INFF.DEBUG) {
    45.             console.log(arguments);
    46.         }
    47.     };
    48.    
    49.     let monkeyPatch = function (
    50.         object,
    51.         methodName,
    52.         newImplementation
    53.     ) {
    54.         object[methodName] = newImplementation.bind(
    55.             object, object[methodName].bind(object)
    56.         );
    57.     };
    58.    
    59.     let classMonkeyPatch = function (
    60.         classFunction,
    61.         methodName,
    62.         newImplementation
    63.     ) {
    64.         monkeyPatch(
    65.             classFunction.prototype,
    66.             methodName,
    67.             newImplementation
    68.         );
    69.     }
    70.    
    71.     monkeyPatch(DataManager, 'isDatabaseLoaded', function (old) {
    72.         return INFF.isLoaded() && old();
    73.     });
    74.    
    75.     monkeyPatch(DataManager, 'isMapLoaded', function (old) {
    76.         return INFF.isLoaded() && old();
    77.     });
    78.    
    79.     monkeyPatch(DataManager, 'extractMetadata', function (old, data) {
    80.         if (data.INFF === undefined) {
    81.             INFF.Jobs.push(data);
    82.             data.INFF = {};
    83.             data.INFF.extractMetadata = old.bind(null, data);
    84.         }
    85.         INFF.parseMetadataForIncludes(data);
    86.     });
    87.    
    88.     INFF.parseMetadataForIncludes = function (data) {
    89.         INFF.debug('parsing', data);
    90.         data.INFF.Status = INFF.Status.Parsing;
    91.         let match = INFF.Regex.exec(data.note);
    92.         if (match !== null) {
    93.             INFF.loadIncludeFromFile(match[0], match[1], data);
    94.         }
    95.         if (data.INFF.Status != INFF.Status.Loading) {
    96.             data.INFF.Status = INFF.Status.Done;
    97.             data.INFF.extractMetadata();
    98.         }
    99.     };
    100.    
    101.     INFF.loadIncludeFromFile = function (includeTag, includeName, data) {
    102.         INFF.debug('loading include', includeTag, includeName, data);
    103.         data.INFF.Status = INFF.Status.Loading;
    104.         if (INFF.Cache[includeName]) {
    105.             INFF.replaceIncludeFromCache(includeTag, includeName, data);
    106.         } else {
    107.             let req = new XMLHttpRequest();
    108.             req.open('GET', 'data/notes/' + includeName + '.txt');
    109.             req.onload = function () {
    110.                 INFF.Cache[includeName] = req.responseText;
    111.                 INFF.replaceIncludeFromCache(includeTag, includeName, data);
    112.             };
    113.             req.onerror = function () {
    114.                 throw new Error(
    115.                     'Failed to load data/notes/' + includeName + '.txt'
    116.                 );
    117.             };
    118.             req.send();
    119.         }
    120.     };
    121.    
    122.     INFF.replaceIncludeFromCache = function (includeTag, includeName, data) {
    123.         INFF.debug('doing replacement', includeTag, includeName, data);
    124.         data.note = data.note.replace(includeTag, INFF.Cache[includeName]);
    125.         INFF.parseMetadataForIncludes(data);
    126.     };
    127.    
    128.     INFF.isLoaded = function () {
    129.         for (let job of INFF.Jobs) {
    130.             if (job.INFF.Status != INFF.Status.Done) {
    131.                 return false;
    132.             }
    133.         }
    134.         return true;
    135.     };
    136.    
    137. })();
    复制代码






    Credit and Thanks

    To reiterate, big thanks to Victor Sant for his original plugin VE_NotesTextFile.js.

    Author's Notes

    NOTE: The following features are not yet available.

    I am considering expanding this plugin to support a feature I'm tentatively deeming load-time variables (LTV). The game data object format ($dataActors[...], $dataItems[...], etc) would be updated to include a property LTV ($dataActors[_].LTV, $dataItems[_].LTV, etc). Values could be inserted into a data object's LTV by including the note tag <define key=value> which would be equivalent to $dataWhatever[_].LTV[key] = value;, and then whenever the expression $(key) is encountered in the notes, value would be interpolated in its place. A note tag <staticinit></staticinit> would also be provided, which would run arbitrary javascript code while loading the data object, and replace the entire note tag and its contents with whatever is returned from the javascript code if it includes a return statement, or with nothing if there is none. Inside <staticinit>, "this" would refer to the data object, so this.LTV would be the current set of load-time variables, in case there's any need to use them programmatically. In case there's a need to just define an LTV programmatically, a variant of define would be provided as <define key></define>, which would follow the same rules as staticinit, except instead of replacing the note tag with the return value if there is any, it would set this.LTV[key] to the return value. Includes would be processed prior to staticinits and defines, so that staticinits and defines would be processed even if they are loaded from inside note mixins. Perhaps I'd even make the entire process fully recursive, so that, e.g., staticinits can return include tags that prompt further loading of note mixins.

    The intention of this prospective update would be to provide a complete load-time scripting system for programmatically initializing data object notes, and, optionally, for further initializing other data object properties based on those notes in certain trivial ways that one might not think would warrant their own plugins.


    本贴来自国际rpgmaker官方论坛作者:_neverGiveUp处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:https://forums.rpgmakerweb.com/threads/load-notes-or-parts-of-notes-from-text-files-includenotesfromfile-js.135396/
    天天去同能,天天有童年!
    回复 送礼论坛版权

    使用道具 举报

    文明发言,和谐互动
    文明发言,和谐互动
    高级模式
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    简体中文
    繁體中文
    English(英语)
    日本語(日语)
    Deutsch(德语)
    Русский язык(俄语)
    بالعربية(阿拉伯语)
    Türkçe(土耳其语)
    Português(葡萄牙语)
    ภาษาไทย(泰国语)
    한어(朝鲜语/韩语)
    Français(法语)
    关闭

    幸运抽奖

    社区每日抽奖来袭,快来试试你是欧皇还是非酋~

    立即查看

    聊天机器人
    Loading...

    QQ|Archiver|手机版|小黑屋|同能RPG制作大师 ( 沪ICP备12027754号-3 )

    GMT+8, 2026-8-1 08:37 , Processed in 0.071669 second(s), 52 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

    快速回复 返回顶部 返回列表