1- using System ;
2- using System . Collections ;
3- using System . IO ;
1+ using System . IO ;
42using System . Linq ;
53using Base_Mod ;
64using JetBrains . Annotations ;
75using UnityEngine ;
8- using UnityEngine . SceneManagement ;
96
107namespace Decal_Loader {
118 [ UsedImplicitly ]
129 public class Plugin : BaseGameMod {
13- protected override string ModName => "Decal-Loader" ;
10+ protected override string ModName => "Decal-Loader" ;
11+ private bool done ;
12+ private DecalCategory category ;
1413
1514 protected override void Init ( ) {
1615 Database . Init ( GetConfigPath ( ) , GetConfigFile ( ) ) ;
1716
1817 base . Init ( ) ;
1918 }
2019
21- protected override void OnIslandSceneLoaded ( Scene scene , LoadSceneMode loadSceneMode ) {
22- base . OnIslandSceneLoaded ( scene , loadSceneMode ) ;
20+ protected override void OnDataSetup ( ) {
21+ base . OnDataSetup ( ) ;
2322
24- var rootGameObject = scene . GetRootGameObjects ( ) [ 0 ] ;
25- if ( ! rootGameObject . HasComponent < LocalDecalLoader > ( ) ) {
26- var localDecalLoader = rootGameObject . AddComponent < LocalDecalLoader > ( ) ;
27- localDecalLoader . loadImagePath = GetConfigPath ( ) ;
28- }
29- }
30- }
31-
32- [ UsedImplicitly ]
33- public class LocalDecalLoader : MonoBehaviour {
34- public string loadImagePath ;
35- private bool done ;
36- private DecalCategory category ;
37-
38- [ UsedImplicitly ]
39- public void Start ( ) {
40- if ( done || ! Directory . Exists ( loadImagePath ) ) return ;
23+ var loadImagePath = GetConfigPath ( ) ;
24+ if ( ! Directory . Exists ( loadImagePath ) ) return ;
4125
4226 category = CreateDecalCategory ( ) ;
4327
@@ -48,17 +32,15 @@ public void Start() {
4832 || file . EndsWith ( ".bmp" ) ) ;
4933
5034 foreach ( var file in files ) {
51- var absoluteUri = new Uri ( file ) . AbsoluteUri ;
52- var request = new WWW ( absoluteUri ) ;
53- StartCoroutine ( WaitForRequest ( request ) ) ;
35+ CreateDecal ( file ) ;
5436 }
5537
5638 done = true ;
5739 }
5840
5941 private static DecalCategory CreateDecalCategory ( ) {
6042 const string name = "Modded" ;
61- var localizedString = new LocalizedString ( ". " , name , name , name ) ;
43+ var localizedString = new LocalizedString ( "decals.category.modded " , name , "." ) ;
6244 var category = ScriptableObject . CreateInstance < DecalCategory > ( ) ;
6345 category . name = localizedString ;
6446 category . NameLocalization = localizedString ;
@@ -74,23 +56,15 @@ private static DecalCategory CreateDecalCategory() {
7456 return category ;
7557 }
7658
77- private IEnumerator WaitForRequest ( WWW request ) {
78- yield return request ;
79-
80- if ( request . error != null ) {
81- Debug . Log ( "Error: Request error." ) ;
82- yield break ;
83- }
84-
85- var uri = request . url ;
86- var guid = Database . instance . GetGuidForUri ( uri ) ;
59+ private void CreateDecal ( string file ) {
60+ var guid = Database . instance . GetGuidForUri ( file ) ;
8761 if ( guid == null ) {
8862 guid = GUID . Create ( ) ;
89- Database . instance . Add ( uri , ( GUID ) guid ) ;
63+ Database . instance . Add ( file , ( GUID ) guid ) ;
9064 }
9165
92- var decalResource = ( DecalResource ) ScriptableObject . CreateInstance ( typeof ( DecalResource ) ) ;
93- decalResource . Resource = request . texture ;
66+ var decalResource = ScriptableObject . CreateInstance < DecalResource > ( ) ;
67+ decalResource . Resource = LoadTexture ( file ) ;
9468 decalResource . Category = category ;
9569
9670 RuntimeAssetStorage . Add ( new [ ] {
@@ -100,7 +74,14 @@ private IEnumerator WaitForRequest(WWW request) {
10074 Labels = new string [ 0 ]
10175 }
10276 } ) ;
103- Debug . Log ( $ "Loaded decal: { uri } .") ;
77+ Debug . Log ( $ "Loaded decal: { file } .") ;
78+ }
79+
80+ private Texture2D LoadTexture ( string file ) {
81+ var fileData = File . ReadAllBytes ( file ) ;
82+ var tex = new Texture2D ( 1 , 1 ) ;
83+ tex . LoadImage ( fileData ) ;
84+ return tex ;
10485 }
10586 }
10687}
0 commit comments