Skip to content

Commit

Permalink
Fallback to the on-disk DLL as Reference if the DLL isn't referenced …
Browse files Browse the repository at this point in the history
…in the solution (#5)

Start enabling automatic detection of the Rimworld DLL when the Solution doesn't reference one
  • Loading branch information
Garethp authored Apr 18, 2023
1 parent 005688c commit 1a2ae0b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 2023.1-Alpha-3
* Fixed some instances of certain classes not being queried for their properties
* If you've got a project without the Assembly-CSharp.dll referenced (If you're on Linux for example), it should now be
able to find the DLL file on disk and reference that directly as a fall-back

## 2023.1-Alpha-2
* Minimum Rider version is now 2023.1
Expand Down
6 changes: 1 addition & 5 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* If we know a bit of XML should be an enum like Gender, we should be able to check that it's a valid enum value
` * Maybe bring out own Rimworld defs if there's no scope?
* Handle Defs with custom classes instead of Rimworld classes
` * Packaging. It shouldn't be too difficult, but I haven't done it yet
* We need to be able to support "LoadAlias", such as "StorageSettings.priority"

* Autocomplete of special types
Expand All @@ -28,13 +27,10 @@
* Make auto completing to other XMLTags look nicer and work faster
* When linking to other def (`<defaultDuty>DefNameHere</defaultDuty>`) also include defs where the tag is a custom class
that extends from the def we're looking for

` * Refactoring
* We're fetching symbol scopes a bit all over the place. Let's collect it into a SymbolScope helper class
`
* Documentation
* Re-read and document References.RimworldXmlReference
* If you have an XML file open while Rider is still initializing, that file doesn't get autocompletion. Document that

* Tests
* It's not a serious plugin project without Tests IMO. Let's at least aim to get one or two unit tests to start with
* It's not a serious project without Tests IMO. Let's at least aim to get one or two unit tests to start with
49 changes: 44 additions & 5 deletions src/dotnet/ReSharperPlugin.RimworldDev/ScopeHelper.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
using System.Collections.Generic;
using System.Linq;
using JetBrains.Application.Threading.Tasks;
using JetBrains.Metadata.Reader.API;
using JetBrains.ProjectModel;
using JetBrains.ProjectModel.model2.Assemblies.Interfaces;
using JetBrains.ReSharper.Psi.Caches;
using JetBrains.ReSharper.Psi.Modules;
using JetBrains.Util;
using JetBrains.Util.Threading.Tasks;

namespace ReSharperPlugin.RimworldDev;

Expand All @@ -12,29 +17,63 @@ public class ScopeHelper
private static ISymbolScope rimworldScope;
private static IPsiModule rimworldModule;
private static List<ISymbolScope> usedScopes;
private static bool adding = false;

public static bool UpdateScopes(ISolution solution)
{
if (solution == null) return false;

allScopes = solution.PsiModules().GetModules().Select(module =>
module.GetPsiServices().Symbols.GetSymbolScope(module, true, true)).ToList();

if (rimworldScope == null)
{
rimworldScope = allScopes.FirstOrDefault(scope => scope.GetTypeElementByCLRName("Verse.ThingDef") != null);

if (rimworldScope == null) return false;
if (rimworldScope == null)
{
AddRef(solution);

return false;
}

rimworldModule = solution.PsiModules().GetModules()
.First(module => module.GetPsiServices().Symbols.GetSymbolScope(module, true, true).GetTypeElementByCLRName("Verse.ThingDef") != null);

// rimworldScope = rimWorldModule.GetPsiServices().Symbols.GetSymbolScope(rimWorldModule, true, true);
.First(module =>
module.GetPsiServices().Symbols.GetSymbolScope(module, true, true)
.GetTypeElementByCLRName("Verse.ThingDef") != null);
}

return true;
}

private static async void AddRef(ISolution solution)
{
if (adding) return;
adding = true;

var locations = new List<string>
{
"C:\\Program Files (x86)\\Steam\\steamapps\\common\\RimWorld\\RimWorldWin64_Data\\Managed\\Assembly-CSharp.dll",
"C:\\Program Files\\Steam\\steamapps\\common\\RimWorld\\RimWorldWin64_Data\\Managed\\Assembly-CSharp.dll",
"~/.steam/steam/SteamApps/common/RimWorld/RimWorldWin64_Data/Managed/Assembly-CSharp.dll"
};


var location = locations.FirstOrDefault(location => FileSystemPath.TryParse(location).ExistsFile);

if (location == null) return;

var path = FileSystemPath.TryParse(location);

var moduleReferenceResolveContext =
(IModuleReferenceResolveContext)UniversalModuleReferenceContext.Instance;

await solution.Locks.Tasks.YieldTo(solution.GetLifetime(), Scheduling.MainDispatcher, TaskPriority.Low);

solution.GetComponent<IAssemblyFactory>().AddRef(path.ToAssemblyLocation(), "ScopeHelper::AddRef",
moduleReferenceResolveContext);
}

public static ISymbolScope RimworldScope => rimworldScope;

public static IPsiModule RimworldModule => rimworldModule;
Expand Down

0 comments on commit 1a2ae0b

Please sign in to comment.