I imported a Unity package where several Editor scripts depend on a RootPath property from a static class:
public static class EditorUtils
{
public static string RootPath => "Assets/HardcodedPath";
}
The problem is that this path is hardcoded, so that the package location can't be changed at all. I plan on changing the RootPath property so that it gets the current script location and searches for parent folder with a given pattern. That way, I would be able to use this package even inside a custom package of my own (inside a Packages folder instead of the Assets folder, that is).
The missing piece is that I can't find a way to get this static class folder location from code. Can anyone help me with this?
Dig around through the AssetDatabase and find a reference to your script. This should return what you're wanting.
public static class EditorUtils
{
public static string RootPath
{
get
{
var g = AssetDatabase.FindAssets ( $"t:Script {nameof(EditorUtils)}" );
return AssetDatabase.GUIDToAssetPath ( g [ 0 ] );
}
}
}
That will also hold true for scripts in Packages.
Remember to change EditorUtils to your script name.
Here's the relevant Unity docs.
Related
Context:
I'm working on a file manager app to help sharing files in a network. To be more specific, there's 2 folders (projects and approved) that exist in local machine and on server, each of these contains subfolders structured like
...\[year]\[project reference]\[subfolder]
once the folder paths are set in the app, I want to add entries to file explorer context menu, to send files from any of the 4 folders to another.
Creating the registry entries was easy, but...
The issue:
I don't know how to start creating the executable to work from the context menu. I suppose I should create a separate console app that takes the selected file paths as arguments, but I don't know to do that exactly (working on visual studio).
Google searches are flooded with the registry creation part; based on the info I could find, my current test attempt is this:
namespace SIDECORE_file_dispatcher
{
static class Program
{
public class User_parameters
{
public string Path_local_proj { get; set; }
public string Path_local_aproved { get; set; }
public string Path_network_proj { get; set; }
public string Path_network_aproved { get; set; }
}
[STAThread]
private static void Main(string filePath) 'filePath being the selected file
{
User_parameters definições = new User_parameters();
xmlLoad<User_parameters> loader = new xmlLoad<User_parameters>();
definições = loader.LoadData("settings.xml");
File.Copy(definições.Path_local_proj + "\\testeficheiro.txt", definições.Path_network_proj + "\\testeficheiro.txt");
MessageBox.Show("executed");
}
}
}
Am I going on the right approach or doing it completely wrong? If you could provide some examples it would be great
I have imported a third-party WSDL (via Service Reference) into my Console Application project in order to send and receive data through Web Services. In order to get this to function appropriately, I have had to add some code to the Reference.cs file associated to the Web Service. While this works, if an update is made to the WSDL, and I re-import/generate that Service Reference, that work-around code will go away.
In order to get around this, I have saved the necessary code-changes to an external text file saved within the project.
I'm curious if anyone knows of a way that I could write these changes into a their own separate class, outside of the Service Reference, and yet, still be referenced by the Service Reference, thus using the "correct" code needed to send/receive from the Web Service.
I have two classes (not included in the generated code) that I am able to reference in the generated code after separating them into their own .cs file and referencing the namespace used by the Service Reference.
What I would like to do, if possible, is the following:
Overall Goal:
Add custom code to code generated by importing a third-party WSDL as a Service Reference, that way when the WSDL is updated by the third-party, another developer would not have to necessarily remember to dive into the Reference.cs file of the Service Reference, and replace/add specific code.
To achieve this goal, I need to be able to:
Replace an existing property and associated field of the generated
partial class, with a customized version (see Snippet #1 below).
Replace an existing generated partial class with a customized version of the class, having a different attribute definition and slightly different property/field definitions.
Snippet #1
Replace the following:
private byte[] bulkExchangeFileField;
[System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:us:gov:treasury:irs:common", DataType = "base64Binary", Order = 0)]
public byte[] BulkExchangeFile
{
get { return this.bulkExchangeFileField; }
set
{
this.bulkExchangeFileField = value;
this.RaisePropertyChanged("BulkExchangeFile");
}
}
with this version of the properties/fields that worked once I altered the generated code:
private BulkExchangeFileType bulkExchangeFileField;
[System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:us:gov:treasury:irs:common", Order = 0)]
public BulkExchangeFileType BulkExchangeFile
{
get { return this.bulkExchangeFileField; }
set
{
this.bulkExchangeFileField = value;
this.RaisePropertyChanged("BulkExchangeFile");
}
}
Use extension methods and/or overload the properties in an inhered class, so your code will not be replaced.
To overload the properties you just need to declare it with the word new before public like in : new public BulkExchangeFileType BulkExchangeFile, so when you use the object it will call your properties instead the ones defined by the web service
and here is how to create extention methods https://msdn.microsoft.com/library/bb383977.aspx
class Program
{
static void Main(string[] args)
{
InheredClass test = new InheredClass(); // Do this
BaseClass test2 = new InheredClass(); // don't do this
Console.WriteLine(test.MyProperty.GetType());
Console.WriteLine(test2.MyProperty.GetType());
Console.Read();
}
class BaseClass
{
public int MyProperty { get; set; }
}
class InheredClass : BaseClass
{
new public decimal MyProperty { get; set; }
}
}
I want to programmatically install a folder in Visual Studio's "Extensions" folder. The closest i can get is using the VS100COMNTOOLS environment variable. What i want to do is go back one level from the "Tools" folder, the go into IDE/Extensions, something like VS100COMNTOOLS..\IDE\Extensions. This is my code:
namespace TemplatesCustomAction
{
public class CustomActions
{
[CustomAction]
public static ActionResult CustomAction1(Session session)
{
var vspath = Environment.GetEnvironmentVariable("VS100COMNTOOLS");
session["VSINSTALLATIONFOLDER"] = string.Format(#"{0}\..\IDE\Extensions", vspath);
return ActionResult.Success;
}
}
}
Use Path.GetFullPath:
var pathWithParent = string.Format(#"{0}\..\IDE\Extensions", vspath);
session["VSINSTALLATIONFOLDER"] = Path.GetFullPath(pathWithParent);
Though I'd also rather use Path.Combine:
var pathWithParent = Path.Combine(vspath, #"\..\IDE\Extensions");
I have this code in my asp.net app:
String appPath = HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath);
String path = appPath + "\\Database\\SAMPLE.BIN";
I want to movie it to a class library, which then will be referenced by multiple projects.
The thing is that HttpContext.Current.Request.ApplicationPath is referencing the running app and not the class lib.
How do I put the SAMPLE.BIN inside the class lib, so that I don't have to duplicate it in all referencing projects?
EDIT:
At the end I need to be able to:
myobject.DatabasePath = appRoot + "\\Database\\SAMPLE.BIN";
You have 2 choices:
Either you pass the value as a parameter to your new library:
public class MyNewLibrary {
private string _path { get; set; }
public MyNewLibrary(string path)
{
this.path = path;
}
}
or you import the System.Web reference and use the HttpContext at will.
I want to host StyleCop in a Custom Environment, the sample code provided in SDK uses this foreach(string myProject in this.myProjects). String doesn't have properties like Path.GetHashCode() and FilesToAnalyze, does anyone knows what is this.myProjects?
List<CodeProject> projects = new List<CodeProject>();
// what is this.myProject?
foreach (string myProject in this.myProjects)
{
CodeProject project = new CodeProject(
myProject.Path.GetHashCode(), myProject.Path, configuration);
// Add each source file to this project.
foreach (string sourceFilePath in myProject.FilesToAnalyze)
{
console.Core.Environment.AddSourceCode(project, sourceFilePath, null);
}
projects.Add(project);
}
After looking at the example in the SDK I think myProject is just a placeholder to indicate how to construct a CodeProject instance.
If you want you can define a class as shown below or keep the root path and files to analyze in a different data structure.
public class MyProject
{
public string Path { get { ... } }
public IEnumerable<string> FilesToAnalyze { get { ... } }
}
Yes, this is just meant to be a pseudocode example. Your best bet it to go to stylecop.codeplex.com and just look at the actual StyleCop code. There are many examples in the code showing how to host StyleCop.