microsoft / xbox-live-samples

This repo contains the samples that demonstrate the API usage patterns for Microsoft Xbox Live Service. These code samples target the Universal Windows Platform (UWP) and Xbox One XDK. To get access to the Xbox Live service, you can join the Xbox Live Creators Program at https://aka.ms/xblcp, or apply to the ID@Xbox program at http://www.xbox.com/en-us/Developers/id
MIT License
144 stars 73 forks source link

Implement C# UWP samples for connecting to xbox live Creators #56

Closed misterjoshm closed 7 years ago

misterjoshm commented 7 years ago

please assign this to me. I'll try to make it work soon!

jasonsandlin commented 7 years ago

Thanks, done

misterjoshm commented 7 years ago

Is there a possibility that you can grant me rights to create a feature branch to work on, so that I can periodically checkin my work for you to review until it's completed?

jasonsandlin commented 7 years ago

In the upper right, click "Fork" to make your own fork. Then clone into that, and you'll have full read/write access. When you done, then submit a pull request. Thanks

misterjoshm commented 7 years ago

I'm still working on this. It's going to take some time. Fast is relative sometimes :).

jasonsandlin commented 7 years ago

No worries. Thanks for the help

misterjoshm commented 7 years ago

No problem. Thanks for the opportunity!

misterjoshm commented 7 years ago

So I just updated my branch strangegraphs_csharp_creators_feature. Can you take a look and see what you think so far? As a start, I am planning to take your current code, and put it into a dll. Then we can expose the necessary functions via the dll in a csharp class library. Then I'll write a test function like a uwp app that does the basic functions. I want to see if you like where I'm going with that before taking the next step and making the necessary functions in the dll, or if it would be better some other way. I'm open for suggestions, but I think this will be the best way to start.

jasonsandlin commented 7 years ago

No need for such complex work. Our WinRT DLL & nuget package already supports C# due to magic of WinRT.

Here's a C# test app that links to our WinRT source: https://github.com/Microsoft/xbox-live-api/tree/master/Tests/CppCxTests/Social/UWP.

This can be change into a C# sample by removing the source linkage and following the "How to link against the XSAPI WinRT UWP source" here: https://github.com/Microsoft/xbox-live-api/blob/master/LINKTOSOURCE.md#how-to-link-against-the-xsapi-winrt-uwp-source

There's more detail in step 2 of: https://docs.microsoft.com/en-us/windows/uwp/xbox-live/get-started-with-creators/develop-creators-title-with-visual-studio

Thanks, Jason

misterjoshm commented 7 years ago

Thanks Jason! I'll start again and look at the way that you suggested!

misterjoshm commented 7 years ago

@jasonsandlin Hi Jason. I made another push with some changes based on your suggestions. Before I go further with it will you look once again. It's much more simple now after your suggestions. I think I can get the rest of the needed functions in the class library relatively easy. I started over and my new branch is called strangegraphs_csharp_xbox_live

jasonsandlin commented 7 years ago

Looks fine, but I personally wouldn't bother writing such thin wrapper apis.
For example, instead of:

    IReadOnlyList<string> GetStat(Page page, XboxLiveContext xboxLiveContext)
     {
         StatisticManager statisticManager = StatisticManager.SingletonInstance;
         if (statisticManager == null)
         {
             return null;
         }
         return statisticManager.GetStatisticNames(xboxLiveContext.User);
     }

It could be just:

    IReadOnlyList<string> GetStat(Page page, XboxLiveContext xboxLiveContext)
    {
        return StatisticManager.SingletonInstance.GetStatisticNames(xboxLiveContext.User);
    }

and then I wouldn't bother with this wrapper function, just call the StatisticManager API directly.

Scenario_WriteStat for example shouldn't always call requestflush. You only want to do that when something important happens, not every time a stat is changed. If you don't call it, it'll upload automatically every 5 mins. If something important happens (level ends, etc) then call request flush.

Same stuff for the other functions.

Personally I would change those functions so the make the API call and then set the data in the sample's UI so they are separate but from the rest of the sample code, but are doing something that isn't just a thin wrap around the WinRT API.

misterjoshm commented 7 years ago

Ok. I'll take the next step then and have the ui do something with the values. I kind of like the wrapper functions because it shows me which functions to call at certain times. Now I'll have it do something in the ui.

misterjoshm commented 7 years ago

I have a semi quick question. I keep getting this error when I try to create an xbox live context object in uwp

Resource Contexts may not be created on threads that do not have a CoreWindow. WinRT information

Here is my snippet of code. Can you give me an idea of what I'm doing wrong? Thanks :)

    public async Task<bool> SignInUser()
    {
        try
        {
            CoreWindow.GetForCurrentThread().Activate();
            XboxLiveUwpImplementation xboxSocialObject = new XboxLiveUwpImplementation();
            var t = (await Windows.System.User.FindAllAsync())[0];
            XboxLiveUser xboxLiveUser = new XboxLiveUser(t);
            var xboxLiveContext = new Microsoft.Xbox.Services.XboxLiveContext(xboxLiveUser);
            return await xboxSocialObject.SignInXboxUser(this, xboxLiveContext);
        }
        catch (Exception ex)
        {

        }
        return false;
    }

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        await SignInUser();
    }
jasonsandlin commented 7 years ago

Pass in the Windows.UI.Xaml.Window.Current.CoreWindow.Dispatcher into SignIn like this C# test app does here:

https://github.com/Microsoft/xbox-live-api/blob/master/Tests/CSharpTestApp/UWP/MainPage.xaml.cs#L152

misterjoshm commented 7 years ago

Thanks Jason!

misterjoshm commented 7 years ago

ok my project was signing people in as of this morning. But now it's crashing. I have to look into that a little bit. I'm going to work on the ui and make a function for each button :)

misterjoshm commented 7 years ago

Get token from IDP failed with ResponseStatus:1

misterjoshm commented 7 years ago

I'll keep at it. I'll get it working better soon :)

misterjoshm commented 7 years ago

I think that it was a problem with visual studio or my computer because when I rebooted everything it started working a little better. Now I'm trying it on the xbox one though and it's having a different error. I'll keep working on it.

misterjoshm commented 7 years ago

WinRT information: Provider error: Unexpected response code: Unknown, Error Code: 0x80131509

misterjoshm commented 7 years ago

I removed my xbox user and re-added it and then the sign in worked :)

misterjoshm commented 7 years ago

xbox live is down so I guess I'll stop for tonight and try again tomorrow :)

misterjoshm commented 7 years ago

Hi Jason. I think that I'm going to restart and take your advice and not use the tiny wrapper functions. It's causing problems that I didn't expect. At least I have more of a background of how it all works now. I'll keep working on it :)

jasonsandlin commented 7 years ago

No worries. You might want to start with the working C# test app that links to our WinRT source: https://github.com/Microsoft/xbox-live-api/tree/master/Tests/CppCxTests/Social/UWP.

To make it a C# sample, removing the source linkage and following the "How to link against the XSAPI WinRT UWP source" here: https://github.com/Microsoft/xbox-live-api/blob/master/LINKTOSOURCE.md#how-to-link-against-the-xsapi-winrt-uwp-source

and then change it as you see fit

misterjoshm commented 7 years ago

sounds good :) Thanks. I'll give it another shot

misterjoshm commented 7 years ago

shoot I took a few days to update my game. I got the sign in part working and my game approved for the creators club... Hey I was thinking though. For this example that I'm putting out, I could maybe clean up my code a bit and put out my game code as an example... It could be a little bit of publicity for my game and maybe help some people get started if they're working in 2d. Of course the art would be copyrighted, but the code they could use as a starting point to use for something else... I've been having a hard time getting approved for id though... But I think games like space whip could really be useful for the xbox platform if they would approve it and maybe even fund it. I can't make something super cool without more options though. It would probably take me at least a couple of weeks to get my game code clean enough to make it easily usable by other people... Do you think that this would be useful? It's pretty basic at the moment. You can see my game in the creators collection on xbox one if you want to check it out sometime, or I could do a more basic example like we have already talked about if you think that would be more useful.

Josh

jasonsandlin commented 7 years ago

Game source is sometimes useful to learn from and sometimes its tricky since they do so much its hard to pull out the pieces that are interesting for other game devs to copy/paste into their project.

That said, you can share whatever you like in your own GitHub repo or elsewhere on the net using whatever license you want and then share links to it. But of course in this Microsoft repo, I can't take pull requests involving copyrighted art or source. For this repo, I prefer to keep it to just samples that show off a single topic with little else so its clear and hopefully easily copy/pasted into other projects.

misterjoshm commented 7 years ago

Makes sense. Thanks :)

misterjoshm commented 7 years ago

I almost have some things to check in

misterjoshm commented 7 years ago

Shoot Some things came up at my job that are requiring more of my attention. I'm still working on it, just really slowly.

jasonsandlin commented 7 years ago

No worries. How about we just close this open issue and you just submit a pull request when your done?

misterjoshm commented 7 years ago

That sounds good. Thanks.

misterjoshm commented 7 years ago

I'll let you close it ok

CartBlanche commented 3 years ago

@strangegraphs Did you ever get leaderboards working in c#?

misterjoshm commented 3 years ago

@CartBlanche I didn't. Sorry about that? @jasonsandlin Hey do you guys want me to give this another shot? I lost track of it.

CartBlanche commented 3 years ago

@strangegraphs that's a pity. My C# game is able to log into Xbox Live. It receives the leaderboard complete event-type message with error code 0, but the moment I try to access the EventArgs property it blows up big time and throws a "No encontrado (404)" exception message. Odd the message is in Spanish. Seems to be coming from the COM interop.

Anyway I'm happy to work with you to get this working, if possible, so other .Net folk don't have the same headaches. Its the last piece I need to get working in my game.