11using System . Diagnostics . CodeAnalysis ;
22using System . IO . Compression ;
3- using Manganese . Array ;
43using Manganese . Text ;
54using ModuleLauncher . NET . Mods . Models . Exceptions ;
65using ModuleLauncher . NET . Mods . Models . Utils ;
@@ -20,16 +19,16 @@ public static async Task<ModInfo> GetModInfoAsync(FileInfo mod)
2019 {
2120 var archive = ZipFile . OpenRead ( mod . FullName ) ;
2221 if ( archive . Entries . Any ( e => e . Name == "mcmod.info" ) )
23- return await GetModInfoLegacyForgeAsync ( mod , archive ) ;
22+ return await GetModInfoLegacyForgeAsync ( archive ) ;
2423 if ( archive . Entries . Any ( e => e . Name == "mods.toml" ) )
25- return await GetModInfoForgeAsync ( mod , archive ) ;
24+ return await GetModInfoForgeAsync ( archive ) ;
2625 if ( archive . Entries . Any ( e => e . Name is "fabric.mod.json" or "quilt.mod.json" ) )
27- return getModInfoFabricAsync ( mod , archive ) ;
26+ return await GetModInfoFabricAsync ( archive ) ;
2827
2928 throw new UnknownModException ( "Unknown mod or mod is corrupted" ) ;
3029 }
3130
32- private static async Task < ModInfo > GetModInfoLegacyForgeAsync ( FileInfo mod , ZipArchive archive )
31+ private static async Task < ModInfo > GetModInfoLegacyForgeAsync ( ZipArchive archive )
3332 {
3433 var zipEntry = archive . GetEntry ( "mcmod.info" ) ! ;
3534 await using var stream = zipEntry . Open ( ) ;
@@ -48,32 +47,57 @@ private static async Task<ModInfo> GetModInfoLegacyForgeAsync(FileInfo mod, ZipA
4847
4948 return re ;
5049 }
51-
52- private static async Task < ModInfo > GetModInfoForgeAsync ( FileInfo mod , ZipArchive archive )
50+
51+ private static async Task < ModInfo > GetModInfoForgeAsync ( ZipArchive archive )
5352 {
5453 var zipEntry = archive . GetEntry ( "META-INF/mods.toml" ) ! ;
5554 await using var stream = zipEntry . Open ( ) ;
5655 using var raw = new StreamReader ( stream ) ;
56+
5757 var tomTable = TOML . Parse ( raw ) ;
5858 var modNode = tomTable [ "mods" ] . AsArray [ 0 ] ;
5959 var re = new ForgeModInfo
6060 {
6161 Id = modNode [ "modId" ] ,
62- Version = modNode [ "version" ] ,
63- Name = modNode [ "displayName" ] ,
64- Url = modNode [ "displayURL" ] ,
65- License = tomTable [ "license" ] ,
66- Description = modNode [ "description" ] ,
67- Authors = modNode . IsArray
68- ? modNode [ "authors" ] . AsArray . RawArray . Select ( x => x . ToString ( ) ) . ToList ( )
69- : new List < string > { modNode [ "authors" ] } ,
62+ Version = modNode [ "version" ] . HasValue ? modNode [ "version" ] : tomTable [ "version" ] ,
63+ Name = modNode [ "displayName" ] . HasValue ? modNode [ "displayName" ] : tomTable [ "displayName" ] ,
64+ Url = modNode [ "displayURL" ] . HasValue ? modNode [ "displayURL" ] : tomTable [ "displayURL" ] ,
65+ License = tomTable [ "license" ] . HasValue ? tomTable [ "license" ] : modNode [ "license" ] ,
66+ Description = modNode [ "description" ] . HasValue ? modNode [ "description" ] : tomTable [ "descrption" ] ,
7067 } ;
71-
68+
69+ if ( modNode [ "authors" ] . HasValue )
70+ re . Authors = modNode [ "authors" ] . IsArray
71+ ? modNode [ "authors" ] . AsArray . RawArray . Select ( x => x . ToString ( ) ) . ToList ( ) !
72+ : new List < string > { modNode [ "authors" ] } ;
73+ else
74+ re . Authors = tomTable [ "authors" ] . IsArray
75+ ? tomTable [ "authors" ] . AsArray . RawArray . Select ( x => x . ToString ( ) ) . ToList ( ) !
76+ : new List < string > { tomTable [ "authors" ] } ;
77+
7278 return re ;
7379 }
7480
75- private static ModInfo getModInfoFabricAsync ( FileInfo mod , ZipArchive archive )
81+ private static async Task < ModInfo > GetModInfoFabricAsync ( ZipArchive archive )
7682 {
77- return new ModInfo ( ) ;
83+ var zipEntry = archive . GetEntry ( "quilt.mod.json" ) ?? archive . GetEntry ( "fabric.mod.json" ) ;
84+ await using var stream = zipEntry ! . Open ( ) ;
85+ using var raw = new StreamReader ( stream ) ;
86+ var content = await raw . ReadToEndAsync ( ) ;
87+
88+ var re = new FabricModInfo
89+ {
90+ Id = content . Fetch ( "id" ) ,
91+ Name = content . Fetch ( "name" ) ,
92+ Description = content . Fetch ( "description" ) ,
93+ Version = content . Fetch ( "version" ) ,
94+ Authors = ( content . FetchJToken ( "authorList" ) ?? content . FetchJToken ( "authors" ) ) ? . Select ( t => t . ToString ( ) ) . ToList ( ) ,
95+ License = content . Fetch ( "license" ) ,
96+ HomePage = content . Fetch ( "contact.homepage" ) ,
97+ Issues = content . Fetch ( "contact.issues" ) ,
98+ Sources = content . Fetch ( "contact.sources" )
99+ } ;
100+
101+ return re ;
78102 }
79103}
0 commit comments