Jump to content

Easiest way to save and load an object? C# Plugin


Mircos

Recommended Posts

Mircos

Hi, I'm about to finish my plugin to add the news section to emby, what I have to do now is to save and load my articles.

 

I already tried to serialize the class and simply save it as byte array, but I get an error when I try to deserialize it because of something about the dll that I didn't fully understand... so, any solution is appreciated, I specify that if you talk about sql I could have some difficulties, but the goal is to have something like "Save(MyArticle)" and "Load(MyArticle)".

 

Thanks!

Link to comment
Share on other sites

Hi there, I don't quite understand what you're trying to do. Can you show some code samples? Thanks.

Link to comment
Share on other sites

Mircos

Ok, let's say that I have this class:

[Serializable]
class MyArticle
{
  string title { get; set;}
  string content { get; set;}
}

Through my plugin some instances of this will be created, so I need to store them in the file system (I'll use Plugin.Instance.DataFolderPath as path).

I tried to give a look at the IFileSystem interface, but I'm not sure about what I should do and I can't find a good example to follow.

 

@@chef

Link to comment
Share on other sites

chef

Ok, let's say that I have this class:

[Serializable]
class MyArticle
{
  string title { get; set;}
  string content { get; set;}
}
Through my plugin some instances of this will be created, so I need to store them in the file system (I'll use Plugin.Instance.DataFolderPath as path).

I tried to give a look at the IFileSystem interface, but I'm not sure about what I should do and I can't find a good example to follow.

 

@@chef

There are really three ways to hold objects permanently for plugins.

 

You can put them in the PluginConfiguration. Which would serialize then to XML. This would allow you to access objects relevant to your plugin.

 

The second way would be to serialize the object to json string, then you could write that to your own plugin file with a .json extension, using a stream writer.

 

If you are trying to build a plugin which creates a channel with media items in it. Then each item you add to the channel would create a table row in the database. Which would also be permanent.

 

What does you plugin do? Perhaps I can better explain a route to take.

Edited by chef
Link to comment
Share on other sites

Mircos

There are really three ways to hold objects permanently for plugins.

 

You can put them in the PluginConfiguration. Which would serialize then to XML. This would allow you to access objects relevant to your plugin.

 

The second way would be to serialize the object to json string, then you could write that to your own plugin file with a .json extension, using a stream writer.

 

If you are trying to build a plugin which creates a channel with media items in it. Then each item you add to the channel would create a table row in the database. Which would also be permanent.

 

What does you plugin do? Perhaps I can better explain a route to take.

 

I'm going to try these ways, I hope to understand how... (yeah, it's the first time that I code a plugin)

 

What my plugin does at the moment is add a new page where users can send requests, I will also add "news section" and maybe I'll try to create a small place to discuss about movies. But for now I'm trying to store permanently the requests

Link to comment
Share on other sites

chef

I see, cool.

 

How would users access this interface?

 

Most plugins configurations are only accessable by admins, or users you give specific access to the dashboard.

 

How will these requests be made?

 

You could create a custom endpoint in the server, and create a separate domain which sends specific information to that endpoint. Title, year etc.

 

Then collect the data that comes across. However opening endpoints and keeping data can be a bit dangerous, so you have to create an ApiClient interface on the domain to access the endpoint with authorization/authentication... Just to be safe.

 

Just a side note here, one time I created a plugin which monitored likes on trailers. When a user liked a trailer they watched, the plugin saw it and sent me a notification. Then, when the Blu-ray was released, It wasripped and added.

 

This is different from sending specific title information because it is only as powerful as the list of trailers in Emby.

Link to comment
Share on other sites

Mircos

I see, cool.

 

How would users access this interface?

 

Most plugins configurations are only accessable by admins, or users you give specific access to the dashboard.

 

How will these requests be made?

 

You could create a custom endpoint in the server, and create a separate domain which sends specific information to that endpoint. Title, year etc.

 

Then collect the data that comes across. However opening endpoints and keeping data can be a bit dangerous, so you have to create an ApiClient interface on the domain to access the endpoint with authorization/authentication... Just to be safe.

 

Just a side note here, one time I created a plugin which monitored likes on trailers. When a user liked a trailer they watched, the plugin saw it and sent me a notification. Then, when the Blu-ray was released, It wasripped and added.

 

This is different from sending specific title information because it is only as powerful as the list of trailers in Emby.

 

Well, at the moment I made it work like this: I added a rest service in the plugin that handles the requests, and I also added a button to the web interface that requests my html page. Security is indeed an issue, a bot could send me tons of requests, but I'm working on that: the idea is to put [Authenticated] on the service and pass the client's access token to the new page through the button in the web interface, if I can't do it safely I'll try other ways.

 

5eaf3e1423a1a_Cattura.png

 

Anyway, I really like your solution about trailers, but I prefer this way because it gives me the opportunity to add any functionality and move all people sending me requests on whatsapp to emby

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...