Changeset 505


Ignore:
Timestamp:
Apr 10, 2025, 3:30:00 PM (3 days ago)
Author:
alloc
Message:

Changed: Load item icons in a coroutine to reduce time needed to get server in a connectable state
Fixed: WebAPI "Player" returns player levels again

Location:
TFP-WebServer
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • TFP-WebServer/WebServer/ModInfo.xml

    r504 r505  
    55        <Description value="Integrated Webserver for the Web Dashboard and server APIs" />
    66        <Author value="The Fun Pimps LLC" />
    7         <Version value="2.0.0.1" />
     7        <Version value="2.0.0.3" />
    88        <Website value="" />
    99</xml>
  • TFP-WebServer/WebServer/src/ModApi.cs

    r504 r505  
    2727
    2828                        if (ItemIconHandler.Instance != null) {
    29                                 ItemIconHandler.Instance.LoadIcons ();
     29                                ThreadManager.StartCoroutine (ItemIconHandler.Instance.LoadIcons ());
    3030                        }
    3131                }
  • TFP-WebServer/WebServer/src/UrlHandlers/ItemIconHandler.cs

    r467 r505  
    11using System;
     2using System.Collections;
    23using System.Collections.Generic;
    34using System.IO;
     
    6869                }
    6970
    70                 public bool LoadIcons () {
     71                private const int LoadingMaxMsPerFrame = 100;
     72                private bool loading;
     73                public IEnumerator LoadIcons () {
    7174                       
    7275                        lock (icons) {
    73                                 if (loaded) {
    74                                         return true;
    75                                 }
     76                                if (loading || loaded) {
     77                                        yield break;
     78                                }
     79
     80                                loading = true;
     81
     82                                MicroStopwatch mswPerFrame = new MicroStopwatch (true);
    7683
    7784                                LoadingStats stats = new LoadingStats ();
     
    99106                                }
    100107
    101                                 try {
    102                                         loadIconsFromFolder (GameIO.GetGameDir ("Data/ItemIcons"), tintedIcons, stats);
    103                                 } catch (Exception e) {
    104                                         Log.Error ("[Web] Failed loading icons from base game");
    105                                         Log.Exception (e);
    106                                 }
     108                                yield return loadIconsFromFolder (GameIO.GetGameDir ("Data/ItemIcons"), tintedIcons, stats, mswPerFrame);
    107109
    108110                                // Load icons from mods
    109111                                foreach (Mod mod in ModManager.GetLoadedMods ()) {
    110                                         try {
    111                                                 string modIconsPath = $"{mod.Path}/ItemIcons";
    112                                                 loadIconsFromFolder (modIconsPath, tintedIcons, stats);
    113                                         } catch (Exception e) {
    114                                                 Log.Error ($"[Web] Failed loading icons from mod {mod.Name}");
    115                                                 Log.Exception (e);
    116                                         }
     112                                        string modIconsPath = $"{mod.Path}/ItemIcons";
     113                                        yield return loadIconsFromFolder (modIconsPath, tintedIcons, stats, mswPerFrame);
    117114                                }
    118115                               
     
    133130                                        Log.Out ($"[Web] IconHandler: Cached {totalSize / 1024} KiB");
    134131                                }
    135 
    136                                 return true;
    137                         }
    138                 }
    139 
    140                 private void loadIconsFromFolder (string _path, Dictionary<string, List<Color>> _tintedIcons, LoadingStats _stats) {
     132                        }
     133                }
     134
     135                private IEnumerator loadIconsFromFolder (string _path, Dictionary<string, List<Color>> _tintedIcons, LoadingStats _stats,
     136                        MicroStopwatch _mswPerFrame) {
    141137                        if (!Directory.Exists (_path)) {
    142                                 return;
    143                         }
     138                                yield break;
     139                        }
     140
     141                        _mswPerFrame.ResetAndRestart ();
    144142
    145143                        foreach (string file in Directory.GetFiles (_path)) {
     
    164162                                        Object.Destroy (tex);
    165163                                } catch (Exception e) {
     164                                        Log.Error ($"[Web] Failed loading icon from {_path}");
    166165                                        Log.Exception (e);
     166                                }
     167
     168                                if (_mswPerFrame.ElapsedMilliseconds >= LoadingMaxMsPerFrame) {
     169                                        yield return null;
     170                                        _mswPerFrame.ResetAndRestart ();
    167171                                }
    168172                        }
  • TFP-WebServer/WebServer/src/WebAPI/APIs/WorldState/Player.cs

    r487 r505  
    158158
    159159                        _writer.WriteRaw (jsonLevelKey);
    160                         _writer.WriteNull (); // TODO
     160                        _writer.WriteInt32 (entity.Progression.Level);
    161161
    162162                        _writer.WriteRaw (jsonHealthKey);
  • TFP-WebServer/bin/Mods/TFP_WebServer/ModInfo.xml

    r504 r505  
    55        <Description value="Integrated Webserver for the Web Dashboard and server APIs" />
    66        <Author value="The Fun Pimps LLC" />
    7         <Version value="2.0.0.1" />
     7        <Version value="2.0.0.3" />
    88        <Website value="" />
    99</xml>
Note: See TracChangeset for help on using the changeset viewer.