@@ -10,18 +10,42 @@ use std::{
10
10
path:: { Path , PathBuf } ,
11
11
} ;
12
12
13
+ /// Represents a scripting language. Languages which compile into another language should use the target language as their language.
14
+ #[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord ) ]
15
+ pub enum Language {
16
+ Rhai ,
17
+ Lua ,
18
+ Rune ,
19
+ External ( Cow < ' static , str > ) ,
20
+ /// Initial setting before being processed by the script synchronization systems
21
+ Unset ,
22
+ /// Set if none of the asset path to language mappers match
23
+ Unknown ,
24
+ }
25
+
26
+ impl std:: fmt:: Display for Language {
27
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
28
+ match self {
29
+ Language :: Rhai => "Rhai" . fmt ( f) ,
30
+ Language :: Lua => "Lua" . fmt ( f) ,
31
+ Language :: Rune => "Rune" . fmt ( f) ,
32
+ Language :: External ( cow) => cow. fmt ( f) ,
33
+ Language :: Unset => "Unset" . fmt ( f) ,
34
+ Language :: Unknown => "Unknown" . fmt ( f) ,
35
+ }
36
+ }
37
+ }
38
+
13
39
/// Represents a script loaded into memory as an asset
14
40
#[ derive( Asset , TypePath , Clone ) ]
15
41
pub struct ScriptAsset {
16
42
pub content : Box < [ u8 ] > ,
17
43
/// The virtual filesystem path of the asset, used to map to the script Id for asset backed scripts
18
44
pub asset_path : PathBuf ,
19
- pub language : Cow < ' static , str > ,
45
+ pub language : Language ,
20
46
}
21
47
22
48
pub struct ScriptAssetLoader {
23
- /// Used to set the language of the script
24
- pub language : Cow < ' static , str > ,
25
49
/// The file extensions this loader should handle
26
50
pub extensions : & ' static [ & ' static str ] ,
27
51
/// preprocessor to run on the script before saving the content to an asset
@@ -52,7 +76,7 @@ impl AssetLoader for ScriptAssetLoader {
52
76
let asset = ScriptAsset {
53
77
content : content. into_boxed_slice ( ) ,
54
78
asset_path : load_context. path ( ) . to_owned ( ) ,
55
- language : self . language . clone ( ) ,
79
+ language : Language :: Unset ,
56
80
} ;
57
81
Ok ( asset)
58
82
}
@@ -62,9 +86,24 @@ impl AssetLoader for ScriptAssetLoader {
62
86
}
63
87
}
64
88
65
- #[ derive( Clone , Copy , Resource ) ]
89
+ #[ derive( Clone , Resource ) ]
66
90
pub struct ScriptAssetSettings {
67
91
pub script_id_mapper : AssetPathToScriptIdMapper ,
92
+ pub script_language_mappers : Vec < AssetPathToLanguageMapper > ,
93
+ }
94
+
95
+ impl ScriptAssetSettings {
96
+ pub fn select_script_language ( & self , path : & Path ) -> Language {
97
+ for mapper in & self . script_language_mappers {
98
+ let language = ( mapper. map ) ( path) ;
99
+ match language {
100
+ Language :: Unset | Language :: Unknown => continue ,
101
+ _ => return language,
102
+ }
103
+ }
104
+
105
+ Language :: Unknown
106
+ }
68
107
}
69
108
70
109
impl Default for ScriptAssetSettings {
@@ -73,6 +112,9 @@ impl Default for ScriptAssetSettings {
73
112
script_id_mapper : AssetPathToScriptIdMapper {
74
113
map : ( |path : & Path | path. to_string_lossy ( ) . into_owned ( ) . into ( ) ) ,
75
114
} ,
115
+ script_language_mappers : vec ! [ AssetPathToLanguageMapper {
116
+ map: ( |_: & Path | Language :: Unset ) ,
117
+ } ] ,
76
118
}
77
119
}
78
120
}
@@ -83,22 +125,33 @@ pub struct AssetPathToScriptIdMapper {
83
125
pub map : fn ( & Path ) -> ScriptId ,
84
126
}
85
127
128
+ #[ derive( Clone , Copy ) ]
129
+ pub struct AssetPathToLanguageMapper {
130
+ pub map : fn ( & Path ) -> Language ,
131
+ }
132
+
86
133
/// A cache of asset id's to their script id's. Necessary since when we drop an asset we won't have the ability to get the path from the asset.
87
134
#[ derive( Default , Debug , Resource ) ]
88
- pub struct AssetIdToScriptIdMap {
89
- pub map : HashMap < AssetId < ScriptAsset > , ScriptId > ,
135
+ pub struct ScriptMetadataStore {
136
+ pub map : HashMap < AssetId < ScriptAsset > , ScriptMetadata > ,
137
+ }
138
+
139
+ #[ derive( Debug , Clone ) ]
140
+ pub struct ScriptMetadata {
141
+ pub script_id : ScriptId ,
142
+ pub language : Language ,
90
143
}
91
144
92
- impl AssetIdToScriptIdMap {
93
- pub fn insert ( & mut self , id : AssetId < ScriptAsset > , path : ScriptId ) {
94
- self . map . insert ( id, path ) ;
145
+ impl ScriptMetadataStore {
146
+ pub fn insert ( & mut self , id : AssetId < ScriptAsset > , meta : ScriptMetadata ) {
147
+ self . map . insert ( id, meta ) ;
95
148
}
96
149
97
- pub fn get ( & self , id : AssetId < ScriptAsset > ) -> Option < & ScriptId > {
150
+ pub fn get ( & self , id : AssetId < ScriptAsset > ) -> Option < & ScriptMetadata > {
98
151
self . map . get ( & id)
99
152
}
100
153
101
- pub fn remove ( & mut self , id : AssetId < ScriptAsset > ) -> Option < ScriptId > {
154
+ pub fn remove ( & mut self , id : AssetId < ScriptAsset > ) -> Option < ScriptMetadata > {
102
155
self . map . remove ( & id)
103
156
}
104
157
}
0 commit comments