@@ -153,13 +153,20 @@ func (plug *EmbedPlugin) GetLevel() MalLevel {
153153 return plug .Level
154154}
155155
156+ // EmbedURI 构建当前嵌入式插件的规范资源URI
157+ func (plug * EmbedPlugin ) EmbedURI (resourcePath string ) string {
158+ rootPath := strings .Trim (plug .RootPath , "/" )
159+ resourcePath = strings .TrimPrefix (resourcePath , "/" )
160+ return fmt .Sprintf ("embed://%s/%s" , rootPath , resourcePath )
161+ }
162+
156163// registerEmbedResourceFunctions 注册嵌入式资源相关的Lua函数
157164func (plug * EmbedPlugin ) registerEmbedResourceFunctions () {
158165 // 重写script_resource函数 - 返回资源文件路径
159166 plug .registerFunction ("script_resource" , func (filename string ) (string , error ) {
160167 resourcePath := "resources/" + filename
161168 if _ , exists := plug .GetFileContent (resourcePath ); exists {
162- return fmt . Sprintf ( "embed://%s/%s" , plug .Name , resourcePath ), nil
169+ return plug .EmbedURI ( resourcePath ), nil
163170 }
164171
165172 // 回退到文件系统
@@ -175,7 +182,7 @@ func (plug *EmbedPlugin) registerEmbedResourceFunctions() {
175182 for _ , levelPlugin := range globalManager .GetEmbeddedPluginsByLevel (level ) {
176183 resourcePath := "resources/" + filename
177184 if _ , fileExists := levelPlugin .GetFileContent (resourcePath ); fileExists {
178- return fmt . Sprintf ( "embed://%s/%s" , levelPlugin .Name , resourcePath ), nil
185+ return levelPlugin .EmbedURI ( resourcePath ), nil
179186 }
180187 }
181188 }
@@ -192,7 +199,7 @@ func (plug *EmbedPlugin) registerEmbedResourceFunctions() {
192199
193200 resourcePath := "resources/" + filename
194201 if _ , exists := plug .GetFileContent (resourcePath ); exists {
195- return fmt . Sprintf ( "embed://%s/%s" , plug .Name , resourcePath ), nil
202+ return plug .EmbedURI ( resourcePath ), nil
196203 }
197204
198205 resourceFile := filepath .Join (assets .GetMalsDir (), plug .Name , "resources" , filename )
@@ -208,7 +215,7 @@ func (plug *EmbedPlugin) registerEmbedResourceFunctions() {
208215 for _ , levelPlugin := range globalManager .GetEmbeddedPluginsByLevel (level ) {
209216 resourcePath := "resources/" + filename
210217 if _ , fileExists := levelPlugin .GetFileContent (resourcePath ); fileExists {
211- return fmt . Sprintf ( "embed://%s/%s" , levelPlugin .Name , resourcePath ), nil
218+ return levelPlugin .EmbedURI ( resourcePath ), nil
212219 }
213220 }
214221 }
@@ -262,35 +269,11 @@ func (plug *EmbedPlugin) registerEmbedResourceFunctions() {
262269 // 新增read_embed_resource函数 - 专门用于读取嵌入式资源,支持embed://路径
263270 plug .registerFunction ("read_embed_resource" , func (resourcePath string ) (string , error ) {
264271 if strings .HasPrefix (resourcePath , "embed://" ) {
265- // 解析嵌入式资源路径: embed://pluginName/resourcePath
266- parts := strings .TrimPrefix (resourcePath , "embed://" )
267- pathParts := strings .SplitN (parts , "/" , 2 )
268- if len (pathParts ) != 2 {
269- return "" , fmt .Errorf ("invalid embedded resource path: %s" , resourcePath )
270- }
271-
272- pluginName := pathParts [0 ]
273- filename := strings .TrimPrefix (pathParts [1 ], "resources/" )
274-
275- // 如果是当前插件的资源
276- if pluginName == plug .Name {
277- resourceFilePath := "resources/" + filename
278- if content , exists := plug .GetFileContent (resourceFilePath ); exists {
279- return string (content ), nil
280- }
281- }
282-
283- // 从全局管理器查找其他插件的资源
284- if globalManager := GetGlobalMalManager (); globalManager != nil {
285- if plugin , exists := globalManager .GetEmbedPlugin (pluginName ); exists {
286- resourceFilePath := "resources/" + filename
287- if content , fileExists := plugin .GetFileContent (resourceFilePath ); fileExists {
288- return string (content ), nil
289- }
290- }
272+ content , err := intl .ReadEmbedResource (resourcePath )
273+ if err != nil {
274+ return "" , err
291275 }
292-
293- return "" , fmt .Errorf ("embedded resource not found: %s" , resourcePath )
276+ return string (content ), nil
294277 }
295278
296279 // 如果不是embed://路径,直接从文件系统读取
0 commit comments